Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
I want to create a measure which refers to another measure value a few months before or later, depending on the value of what-if parameter. I would like to even apply different what-if parameter based on the value of a certain dimension.
I could apply a single what-if parameter using the DAX below. "Shifted Cost" measure refers to "Cost" measure in two months before, which is from "Offset A" what-if parameter. Now, I want to use different what-if parameter - "Offset A" for Project A and "Offset B" for Project B. How can I achieve that?
Solved! Go to Solution.
So I could not make IF() work, but I found a solution.
In this way, everything looks perfect as below. I am not sure if this is the best solution, though.
So I could not make IF() work, but I found a solution.
In this way, everything looks perfect as below. I am not sure if this is the best solution, though.
@Kazu , I gave it another try and indeed the difficulty is to determine the offset value for the parr period when the filter context is on an aggregate level, for this we need to know the category on a lower level, I first tried something like this again:
SalesWithOffsetWrong =
VAR _OffsetA = 'Offset A'[Offset A Value]
VAR _OffsetB = 'Offset B'[Offset B Value]
RETURN
CONCATENATEX(
CALCULATETABLE(
SUMMARIZE (
'Table';
'Table'[Category];
'Date'[Date];
"OffsetSales"; CALCULATE ( SUM ( 'Table'[Sales] ) )
);
PARALLELPERIOD ( // this is a context filter, category is not known because its not a row level, on higher aggregation level this gives issues.
'Date'[Date];IF(SELECTEDVALUE('Table'[Category])="A";_OffsetA;_OffsetB);
MONTH
));
[OffsetSales]
)
But that won't work because the category is not known in the aggregate level. So I figured something needs to be done on row level, like is done with a filter.
Therefore, I ended up with the following working code:
SalesWithOffsetRight =
VAR _OffsetA = 'Offset A'[Offset A Value]
VAR _OffsetB = 'Offset B'[Offset B Value]
RETURN
CALCULATE (
SUMX (
FILTER ( 'Table'; 'Table'[Category] = "A" );
CALCULATE ( SUM ( 'Table'[Sales] ) )
);
PARALLELPERIOD ( 'Date'[Date]; _OffsetA; MONTH )
)
+ CALCULATE (
SUMX (
FILTER ( 'Table'; 'Table'[Category] = "B" );
CALCULATE ( SUM ( 'Table'[Sales] ) )
);
PARALLELPERIOD ( 'Date'[Date]; _OffsetB; MONTH )
)
This gives the right results without the use of a helper table.
Link to the file is the same.
Perhaps this answers your original question? If so, pls mark as such.
Kind regards, Steve.
Proud to be a Super User!
Awesome Keyboard Shortcusts in Power BI, thumbs up if you like the article
My Community Blog Articles (check them out!)
My Blog - Power M code to automatically detect column types -
How to create test data using DAX!
You can add an IF() variable to get your desired result with a pattern like this:
Shifted Cost = var __projectoffset = SWITCH(SELECTEDVALUE(Main[Project]), "A", [Offset A], "B", [Offset B], BLANK())
Return (your measure with __projectoffset in place of [Offset A Value] measure)
If this works for you, please mark it as the solution. Kudos are appreciated too. Please let me know if not.
Regards,
Pat
To learn more about Power BI, follow me on Twitter or subscribe on YouTube.
Thank you @mahoneypat ,
Here is what I tried. Two issues.
1. The value cannot be shifted to the intersection which does not exist before shifting. A little hard to explain for me... the Project A -June and Project B Jan below.
2. Total is totally wrong. I think this is because the shifting by parallelperiod happens after the aggregation by SUMX, but don't know how I can shift and then aggregate.
Encapsulate the Offset part in an If statement (in case you only have projects A and B) or add another measure that combines the project and whatif filters (for more flexibility with more than two projects)
@Kazu ; I see your challenge, I got it working with some trickery, not the most elegant solution, but is working with a measure for month level and one to aggregate:
FinalOffSetValue =
IF(ISFILTERED('Date'[Date].[Month]);[SalesWithOffsetDayLevel];[SalesWithOffsetMonthOrAbove])
SalesWithOffsetMonthOrAbove =
SUMX(
VALUES('Table'[Date]);
var _date = SELECTEDVALUE('Table'[Date])
var _Category = SELECTEDVALUE('Table'[Category];"Multiple")
var _CT =
CALCULATETABLE(
SUMMARIZE('Table';'Table'[Date];"OffsetSales";CALCULATE(SUM('Table'[Sales])));
PARALLELPERIOD('Table'[Date];
SWITCH(_Category;"A";'Offset A'[Offset A Value];
"B";'Offset B'[Offset B Value])
;MONTH))
return
SUMX(_CT;[OffsetSales])
)
SalesWithOffsetDayLevel =
CALCULATE(SUM('Table'[Sales]);
PARALLELPERIOD('Date'[Date];
SWITCH(SELECTEDVALUE('Table'[Category]);"A";'Offset A'[Offset A Value];
"B";'Offset B'[Offset B Value])
;MONTH))
Working as seen here:
So FinalOffSetValue is displaying the right value on all levels.
Power BI file is here.
Does it solve it? Please mark as solution. Does it help? Thumbs up would be great.
p.s. where the labels say, day level, I mean month level.
Kind regards,
Steve.
Proud to be a Super User!
Awesome Keyboard Shortcusts in Power BI, thumbs up if you like the article
My Community Blog Articles (check them out!)
My Blog - Power M code to automatically detect column types -
How to create test data using DAX!
Thanks @stevedep and sorry for the late response.
It seems your formula works for the total in time dimension, but not for other dimension (e.g. total of A+B would be wrong). That said, it was good to know that it is not for me that this is difficult.
Thank you @lbendlin
I am not sure I understand your suggestions correctly, but I tried below, which acutally I knew should not work. I am new to DAX and still struggling to get away from tabluar way of thinking, but I think the reason why neither below works is because the selection of what-if parameter is done after the aggregation by SUMX. Instead, I want parameter selection to be done before SUMX.
I first tried IF statement like:
A single value for column 'Project' in table 'Main' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result. |
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
43 | |
20 | |
20 | |
16 | |
15 |
User | Count |
---|---|
46 | |
28 | |
25 | |
18 | |
17 |