Power BI : how to create measure that refer other measure (DAX) - matrix

i try to create a matrix visual with measure as column and the KPI as row.
the issue is occur when the measure should accommodate each KPI calculation, so i think that create a measure that refer other measure(each KPI calculation) is the solution. please find the detail on below your advise would be greatly appreciated. thank you in advance
Here is the visual expectation
Here is the data that i prepare
this is one the KPI measure. that i would like to call/ refer on my "Column" Measure
LI MTD LY SOH Days DL MockUp =
var SOHDaysCPD = CALCULATE(SUM('DataLake KPI Summary'[MTD Value]),ALL(Dim_Date[Date]),
'DataLake KPI Summary'[Cube]="SOH (days)",'DataLake KPI Summary'[Division]="Consumer Products",'DataLake KPI Summary'[Year]=2022
)
var SOHDaysPPD = CALCULATE(SUM('DataLake KPI Summary'[MTD Value]),ALL(Dim_Date[Date]),
'DataLake KPI Summary'[Cube]="SOH (days)",'DataLake KPI Summary'[Division]="Professional Products",'DataLake KPI Summary'[Year]=2022
)
var SOHDaysLLD = CALCULATE(SUM('DataLake KPI Summary'[MTD Value]),ALL(Dim_Date[Date]),
'DataLake KPI Summary'[Cube]="SOH (days)",'DataLake KPI Summary'[Division]="L'Oreal Luxe",'DataLake KPI Summary'[Year]=2022
)
var SOHDaysACD = CALCULATE(SUM('DataLake KPI Summary'[MTD Value]),ALL(Dim_Date[Date]),
'DataLake KPI Summary'[Cube]="SOH (days)",'DataLake KPI Summary'[Division]="Active Cosmetics",'DataLake KPI Summary'[Year]=2022
)
var SOHValueLI = CALCULATE(SUM('DataLake KPI Summary'[MTD Value]),ALL(Dim_Date[Date]),
'DataLake KPI Summary'[Cube]="B2B SOH (M)",'DataLake KPI Summary'[Year]=2022
)
var SOHValueCPD = CALCULATE(SUM('DataLake KPI Summary'[MTD Value]),ALL(Dim_Date[Date]),
'DataLake KPI Summary'[Cube]="B2B SOH (M)",'DataLake KPI Summary'[Division]="Consumer Products",'DataLake KPI Summary'[Year]=2022
)
var SOHValuePPD = CALCULATE(SUM('DataLake KPI Summary'[MTD Value]),ALL(Dim_Date[Date]),
'DataLake KPI Summary'[Cube]="B2B SOH (M)",'DataLake KPI Summary'[Division]="Professional Products",'DataLake KPI Summary'[Year]=2022
)
var SOHValueLLD = CALCULATE(SUM('DataLake KPI Summary'[MTD Value]),ALL(Dim_Date[Date]),
'DataLake KPI Summary'[Cube]="B2B SOH (M)",'DataLake KPI Summary'[Division]="L'Oreal Luxe",'DataLake KPI Summary'[Year]=2022
)
var SOHValueACD = CALCULATE(SUM('DataLake KPI Summary'[MTD Value]),ALL(Dim_Date[Date]),
'DataLake KPI Summary'[Cube]="B2B SOH (M)",'DataLake KPI Summary'[Division]="Active Cosmetics",'DataLake KPI Summary'[Year]=2022
)
var CPD = SOHValueCPD / SOHDaysCPD
var PPD = SOHValuePPD / SOHDaysPPD
var LLD = SOHValueLLD / SOHDaysLLD
var ACD = SOHValueACD / SOHDaysACD
return
SOHValueLI/(CPD+PPD+LLD+ACD)
this is the "Column" Measure that i expect could add some syntax formula that refer to KPI measure
MTD LY DL MockUp =
var val = TOTALMTD(SUM('DataLake KPI Summary'[MTD Value]),DATEADD(Dim_Date[Date],-1,year))
var val1 = TOTALMTD(AVERAGE('DataLake KPI Summary'[MTD Value]),DATEADD(Dim_Date[Date],-1,year))
return
IF(SELECTEDVALUE('DataLake KPI Summary'[Cube]) ="Service Rate (%)" || SELECTEDVALUE('DataLake KPI Summary'[Cube]) ="SI FCA (M-3) (%)"|| SELECTEDVALUE('DataLake KPI Summary'[Cube]) ="SI FCA (M-3) (%)"|| SELECTEDVALUE('DataLake KPI Summary'[Cube]) ="SI FCA (M-3) (%)"|| SELECTEDVALUE('DataLake KPI Summary'[Cube]) ="SI FCA Bias (%)"|| SELECTEDVALUE('DataLake KPI Summary'[Cube]) ="SO FCA (M-3) (%)"|| SELECTEDVALUE('DataLake KPI Summary'[Cube]) ="SO FCA Bias (%)"|| SELECTEDVALUE('DataLake KPI Summary'[Cube]) ="CDC Country IQ rate (%)"|| SELECTEDVALUE('DataLake KPI Summary'[Cube]) ="Avg. Total Inventory / CNS (%)"|| SELECTEDVALUE('DataLake KPI Summary'[Cube]) ="EOM. Credit/CNS (%)"|| SELECTEDVALUE('DataLake KPI Summary'[Cube]) ="Overdue / total credit (%)"|| SELECTEDVALUE('DataLake KPI Summary'[Cube]) ="Avg Overdue / total credit (%)",FORMAT( val1,"0.0%"),
SWITCH ( TRUE(),
val > 1000, FORMAT ( val / 1000,"0.0" & "B" ),val))

Related

Created table in DAX, but can't extract the correct value

I'm attempting to create a measure to give me the rank of a Salesman by Sales. I'll use the measure in a table where one of the columns is Salesman, so there should be the appropriate row context applied. But using
RETURN Calculate( MAXX (RankTable, [Rank] ) )
just gives me a value of 1 for every broker. I can't figure out what to used to just pull out the value for Rank calculated in RankTable. How do I do that?
SumSales = sumx( SalesData, [Sale])
----------
SalesRank =
VAR SummaryTable =
ADDCOLUMNS(
SUMMARIZE( SalesData, [Salesman] ),
"Sales", [SumSales]
)
VAR RankTable =
ADDCOLUMNS(
SummaryTable,
"Rank", RANKX( SummaryTable, [Sales])
)
RETURN
Calculate( MAXX (RankTable, [Rank] ) )
I know that RankTable is correct, since DAX Studio give me this result:
Salesman Sales Rank
A 907 1
B 600 3
C 900 2
D 500 4
Here's code for measure mentioned in comments:
Priced. =
COUNTROWS(
FILTER( 'Cases',
[Date Initiated] >= [MinDate]
&& [Date Initiated] <= [MaxDate]
&& not ISBLANK( [Date To Pricing] )
)
)
I think you might be overcomplicating things, this should work:
SalesRank = RANKX(ALL('Salesdata'[Salesman]), [SumSales])
Reason you're getting 1 for every line is because in a visual the measure is calculated in the context containing a single salesman. So you need to remove this filter using the ALL() function.

DAX Measure to Calculate Total Yearly Forecast by Snapshot Period

I want to recreate a sumifs dax measure (not calculated column) to Sum Yearly Totals for each snapshot period (less than or equal to selected slicer Month End) by (forecast)month and Productcategory
In Excel this would be accomplished with the following sumifs function =SUMIFS($D$2:$D$97,$A$2:$A$97,B2,$C$2:$C$97,C2)
That is sum amount if reporting period equals value of month in current table row and category equals value of category in current table row
Table fields:
Snapshot Date (range from 31JAN2020 to 31DEC2020 by month end)
Forecast for Month (range from 31JAN2020 to 31DEC2020 by month end)
Sub Category (bikes, coats)
Amount (number actual sales mtd or projected count of sales)
Type (Actual or projection) actual sales amount to date, or projected for each month to end of year
Snapshot Date,Forecast for Month,Sub Category,Amount,Type,Expected Result fo 31MAR2020
1/31/2020,1/31/2020,Bikes,3,Actual,133
1/31/2020,2/29/2020,Bikes,7,Projection,138
1/31/2020,3/31/2020,Bikes,11,Projection,0
1/31/2020,4/30/2020,Bikes,20,Projection,0
1/31/2020,5/31/2020,Bikes,10,Projection,0
1/31/2020,6/30/2020,Bikes,11,Projection,0
1/31/2020,7/31/2020,Bikes,20,Projection,0
1/31/2020,8/31/2020,Bikes,20,Projection,0
1/31/2020,9/30/2020,Bikes,2,Projection,0
1/31/2020,10/31/2020,Bikes,9,Projection,0
1/31/2020,11/30/2020,Bikes,18,Projection,0
1/31/2020,12/31/2020,Bikes,2,Projection,0
1/31/2020,1/31/2020,Coats,1,Actual,111
1/31/2020,2/29/2020,Coats,8,Projection,136
1/31/2020,3/31/2020,Coats,10,Projection,0
1/31/2020,4/30/2020,Coats,17,Projection,0
1/31/2020,5/31/2020,Coats,12,Projection,0
1/31/2020,6/30/2020,Coats,1,Projection,0
1/31/2020,7/31/2020,Coats,11,Projection,0
1/31/2020,8/31/2020,Coats,4,Projection,0
1/31/2020,9/30/2020,Coats,16,Projection,0
1/31/2020,10/31/2020,Coats,10,Projection,0
1/31/2020,11/30/2020,Coats,10,Projection,0
1/31/2020,12/31/2020,Coats,11,Projection,0
2/29/2020,1/31/2020,Bikes,3,Actual,133
2/29/2020,2/29/2020,Bikes,17,Actual,138
2/29/2020,3/31/2020,Bikes,7,Projection,0
2/29/2020,4/30/2020,Bikes,17,Projection,0
2/29/2020,5/31/2020,Bikes,11,Projection,0
2/29/2020,6/30/2020,Bikes,11,Projection,0
2/29/2020,7/31/2020,Bikes,18,Projection,0
2/29/2020,8/31/2020,Bikes,17,Projection,0
2/29/2020,9/30/2020,Bikes,3,Projection,0
2/29/2020,10/31/2020,Bikes,13,Projection,0
2/29/2020,11/30/2020,Bikes,9,Projection,0
2/29/2020,12/31/2020,Bikes,12,Projection,0
2/29/2020,1/31/2020,Coats,15,Actual,111
2/29/2020,2/29/2020,Coats,7,Actual,136
2/29/2020,3/31/2020,Coats,15,Projection,0
2/29/2020,4/30/2020,Coats,11,Projection,0
2/29/2020,5/31/2020,Coats,1,Projection,0
2/29/2020,6/30/2020,Coats,12,Projection,0
2/29/2020,7/31/2020,Coats,9,Projection,0
2/29/2020,8/31/2020,Coats,13,Projection,0
2/29/2020,9/30/2020,Coats,2,Projection,0
2/29/2020,10/31/2020,Coats,16,Projection,0
2/29/2020,11/30/2020,Coats,19,Projection,0
2/29/2020,12/31/2020,Coats,16,Projection,0
Any help would be appreciated
This is a possible measure that emulates the sumifs expression.
It work at the level of the single row and returns BLANK() if more than one row is in the current filter context
Result =
VAR SelectedMonth =
SELECTEDVALUE ( Snapshot[Forecast for Month] )
VAR SelectedSubCategory =
SELECTEDVALUE ( Snapshot[Sub Category] )
RETURN
IF (
NOT ISBLANK ( SelectedMonth )
&& NOT ISBLANK ( SelectedSubCategory ),
CALCULATE (
SUM ( Snapshot[Amount] ),
REMOVEFILTERS ( Snapshot ),
Snapshot[Snapshot Date] = SelectedMonth,
Snapshot[Sub Category] = SelectedSubCategory
) + 0
)

How to calculate a measure based on a decreasing/increasing column's value in DAX

I have a Sales table and related dimension tables. MySales table contains columns : Week, StoreID, SalesSeasonID, ProductKey and metrics. My dimensions are related to sales table (Date,SalesSeason,Store,Product tables).
I need to find Sales Quantity (LastYear and LastSeason), as a measure
You can find a sample below:
My purpose is when user selected SaleseasonID[4] then it will return 2 as SalesQuantity.
How can I calculate this measure by DAX formula?
Try:
PYSales =
SUMX (
VALUES ( Table1[YearWeek] ),
CALCULATE (
SUM ( Table1[SalesQuantity] ),
ALL ( Table1[SalesSeasonID] ),
FILTER (
ALL ( Table1[YearWeek] ),
Table1[YearWeek] = EARLIER ( Table1[YearWeek] ) - 100
)
)
)
Worked example PBIX file, using your sample data: https://pwrbi.com/so_55703551/

DAX How do I calculate Exchange Rate between two tables?

I have a table Sales with fields: Date, Revenue, and CurrencyCode
I have a table ExchangeRate with fields ValidFrom, ValidTo, ExchangeRate, and CurrencyCode.
I need to multiply revenue by the ExchangeRate, when the sales Date falls between ValidFrom and ValidTo and the CurrencyCode from both tables match.
You can create a calculate column in the Sales table to take the Rate from ExchangeRate table then just multiply Rate by the Revenue value in an additional column or a measure.
Use this expression in the Sales calculated column, I named ExchangeRate
ExchangeRate =
CALCULATE (
MAX ( ExchangeRate[ExchangeRate] ),
FILTER (
FILTER ( ExchangeRate, [CurrencyCode] = EARLIER ( Sales[CurrencyCode] ) ),
[ValidFrom] <= EARLIER ( Sales[Date] )
&& [ValidTo] >= EARLIER ( Sales[Date] )
)
)
Now you can create a measure for multiplying the Rate by the Revenune:
RevenueXRateMeasure = SUM(Sales[Revenue])*SUM(Sales[ExchangeRate])
Or if you prefer a column use:
RevenueXRateColumn = [Revenue]*[ExchangeRate]
Let me know if this helps.

How can I create a pie chart in a report combining two datasets in Visual Studio?

My question is exactly the one I used for title. I've got an .rdl file with two datasets that are really hard to combine in one script, so I want to create a pie chart using both of them as individual datasets. I need to find the percentages of an addition of two things (one from the first dataset and one from the second), so basically i need this addition and then a division like the next one:
(first_thing+second_thing)/(sum(first_thing)+sum(second_thing))
for example I have dog_sales in the one table and cat_sales on the other and I need to do a pie chart with the percentages of the sales of both dogs and cats for this month.
How can I do it?
I don't know of a way to join two datasets together in one pie chart. Instead I would recommend joining your datasets into one new dataset that can be used by the pie chart.
Using your example of cat vs dog sales by month I have created an example of how to join 2 datasets together.
You can use the "total_sales" column in the final query to populate your pie chart values and you can use "month_of_sale" as the series group.
-- Create temp table to store Cat sales data
DECLARE #catSales TABLE(
Sales INT
, MonthOfSale INT);
-- Create temp table to store Dog sales data
DECLARE #dogSales TABLE(
Sales INT
, MonthOfSale INT)
--Populate cat sales table with data
INSERT INTO #catSales
( Sales, MonthOfSale )
VALUES ( 50, -- Sales - int
1 -- MonthOfSale - int
)
INSERT INTO #catSales
( Sales, MonthOfSale )
VALUES ( 100, -- Sales - int
2 -- MonthOfSale - int
)
INSERT INTO #catSales
( Sales, MonthOfSale )
VALUES ( 75, -- Sales - int
3 -- MonthOfSale - int
)
--Populate dog sales table with data
INSERT INTO #dogSales
( Sales, MonthOfSale )
VALUES ( 150, -- Sales - int
1 -- MonthOfSale - int
)
INSERT INTO #dogSales
( Sales, MonthOfSale )
VALUES ( 80, -- Sales - int
3 -- MonthOfSale - int
)
INSERT INTO #dogSales
( Sales, MonthOfSale )
VALUES ( 200, -- Sales - int
4 -- MonthOfSale - int
)
--View data in cat sales table (note that months 1, 2, and 3 are all populated with data, but not month 4.
SELECT Sales
, MonthOfSale
FROM #catSales
--View data in dog sales table (note that months 1, 3, and 4 are all populated with data, but not month 2.
SELECT Sales
, MonthOfSale
FROM #dogSales
--Join the data from Cat and Dog sales together based on the month
SELECT cs.sales AS 'cat_sales'
, ds.sales AS 'dog_sales'
, ISNULL(cs.sales, 0) + ISNULL(ds.sales, 0) AS 'total_sales' -- Use ISNULL to convert missing data into 0 so that it adds correctly
, ISNULL(cs.MonthOfSale, ds.MonthOfSale) AS 'month_of_sale' -- If there are not cat sales, then the cs.MonthOfSale will be null and ds.MonthOfSale should be used instead
FROM #catSales cs
FULL OUTER JOIN #dogSales ds ON cs.MonthOfSale = ds.MonthOfSale -- Use full outer join to include all data from both tables

Resources