Forum Discussion
Current vs Previous value, not dependent on base value
- 7 years ago
Hi Anonymous
I gave you some erroneous code previously (I hadn't tested it - i was making extrapolations from other similar work I've done, but I had some false assumptions).
Points 1-3 in the above reply still hold - please review them. However, now we need to modify the measures.
For transparency, we'll use 3 measures. I'll talk about where they can be combined afterwards
TodaysValue= SWITCH( [MType]; "BJ-HÖ1"; AVERAGE(Blad1[BJ-HÖ1]); "BJ-MÄ"; AVERAGE(Blad1[BJ-MÄ]); "BJ-TS1"; AVERAGE(Blad1[BJ-TS1]); etc ) PrevDayValue = CALCULATE([TodaysValue]; PREVIOUSDAY(DateTimeTab[Date])) Measure Delta = [TodaysValue] - [PrevDayValue]The mistake was thinking we could store some of the complex code in variables and use the code again further down in the measure. I think we can really only have a minimum of 2 measures, unless you want to replicate a lot of code:
TodaysValue= SWITCH( [MType]; "BJ-HÖ1"; AVERAGE(Blad1[BJ-HÖ1]); "BJ-MÄ"; AVERAGE(Blad1[BJ-MÄ]); "BJ-TS1"; AVERAGE(Blad1[BJ-TS1]); etc ) Measure Delta =
//NOTE That variables are not needed here - just showing for illustrative purposes VAR __Todaysvalue = [TodaysValue]
VAR __PrevValue = CALCULATE([TodaysValue], PREVIOUSDAY(DateTimeTab[Date]) RETURN __Todaysvalue - __PrevValue
//You could do the following without any variables and get the same results
//Measure Delta = [TodaysValue] - CALCULATE([TodaysValue], PREVIOUSDAY(DateTimeTab[Date])Hope this finally gets you to where you need to be. Sorry for the earlier confusion.
David
It was indeend an extra paranthesis... but I only get a "0" returned as a resulst in the graphs
Measure Delta = var __SelectedMeasure =
SWITCH(
[MType];
"BJ-HÖ1"; AVERAGE(Blad1[BJ-HÖ1]);
"BJ-MÄ"; AVERAGE(Blad1[BJ-MÄ]);
"BJ-TS1"; AVERAGE(Blad1[BJ-TS1]);
etc
)
var __PDMeasure = CALCULATE(__SelectedMeasure; PREVIOUSDAY(Blad1[Tid]))
RETURN __SelectedMeasure - __PDMeasureI then have measure called Disp that is the same as the code above without the vars and Return, så just the switch between the values.
In the second table i have:
MType = SELECTEDVALUE(SelectValue[ValueSelect])
and the column with the names of the different value "categories" (as seen in the code, BJ-HÖ1 etc)
The Slicer is coupled with this table column (Called ValueSelect) and properly changes the displayed values in my chart.
So maybe I missunderstood something regarding where to put the calucation vars?
"The Slicer is coupled with this table column (Called ValueSelect) and properly changes the displayed values in my chart. "
If by "coupled" you mean "having a relationship in the model", you do not want the slicer value table to have a relationship with the fact table. The slicer table is independent of anything else so it doesn't introduce any extra filters.
It shouldn't matter where the measures live, but best practice would be to put it either on the fact table (Blad1) or in a separate Measures table.
One other thing I notice is that you are using PREVIOUSDAY() on the date field that is on the fact table. PREVIOUSDAY() (and most of time intellegence functions) should be done against a Date table that has a 1-to-many relationship to the fact table. CALENDARAUTO() is the quickest way to make a Date table if you don't already have one.
- dedelman_clng7 years agoCommunity Champion
Hi Anonymous
I gave you some erroneous code previously (I hadn't tested it - i was making extrapolations from other similar work I've done, but I had some false assumptions).
Points 1-3 in the above reply still hold - please review them. However, now we need to modify the measures.
For transparency, we'll use 3 measures. I'll talk about where they can be combined afterwards
TodaysValue= SWITCH( [MType]; "BJ-HÖ1"; AVERAGE(Blad1[BJ-HÖ1]); "BJ-MÄ"; AVERAGE(Blad1[BJ-MÄ]); "BJ-TS1"; AVERAGE(Blad1[BJ-TS1]); etc ) PrevDayValue = CALCULATE([TodaysValue]; PREVIOUSDAY(DateTimeTab[Date])) Measure Delta = [TodaysValue] - [PrevDayValue]The mistake was thinking we could store some of the complex code in variables and use the code again further down in the measure. I think we can really only have a minimum of 2 measures, unless you want to replicate a lot of code:
TodaysValue= SWITCH( [MType]; "BJ-HÖ1"; AVERAGE(Blad1[BJ-HÖ1]); "BJ-MÄ"; AVERAGE(Blad1[BJ-MÄ]); "BJ-TS1"; AVERAGE(Blad1[BJ-TS1]); etc ) Measure Delta =
//NOTE That variables are not needed here - just showing for illustrative purposes VAR __Todaysvalue = [TodaysValue]
VAR __PrevValue = CALCULATE([TodaysValue], PREVIOUSDAY(DateTimeTab[Date]) RETURN __Todaysvalue - __PrevValue
//You could do the following without any variables and get the same results
//Measure Delta = [TodaysValue] - CALCULATE([TodaysValue], PREVIOUSDAY(DateTimeTab[Date])Hope this finally gets you to where you need to be. Sorry for the earlier confusion.
David
- dedelman_clng7 years agoCommunity Champion
Check that the data for the value names doesn't have white space or special characters. The match needs to be exact with what you put in the SWITCH() statement.
- Anonymous7 years agoNot applicable
Thank you for the speedy replies :)
Maybe coupled was a poor choice of words, I meant that it is selected in the slicer,
So if I understood right, I made a new table using calendarauto and gave it a 1 - * relationship with the table containing my "original" time column and the values.
but when I use this new tables date column I get a blank image on my chart? The Original time still works.
- dedelman_clng7 years agoCommunity Champion
Is Blad1[Tid] meant to be a time value or a date+time value or something else?
- Anonymous7 years agoNot applicable
Date and time, YYYY-MM-DD HH-MM:SS every value is from 02:00:00
- dedelman_clng7 years agoCommunity Champion
At this point I don't know if I can go further without seeing a copy of your actual PBIX file. If you can remove or mask sensitive data and put it on a sharing site, I can get a copy and see what is going on.
Alternatively, with a sample of data and a view of the model I may be able to reconstruct it without the need for you to send the entire thing.
- Anonymous7 years agoNot applicable
Hi,
Thank you for your continued help,
I made a version with new names, I've uploaded the file to dropbox: PBIX file
Thanks
- dedelman_clng7 years agoCommunity Champion
Hi Anonymous -
Several observations on the fiel you sent:
1) The data prior to 1-Jul-2017 is all blank. You may want to look at filtering that out in the query. Also, many, many rows have a blank Date value
2) When creating any visuals (while doing Time Intelligence functions), use the date field from your calendar, not from the fact table.
3) That being said, the Date field in your fact table (Blad1) has 2:00 AM for all values. CALENDARAUTO() defaults to 12:00 AM for all dates, therefore even though you have created a relationship, there are no records that match between DateTimeTable and Blad1. You have two options here - strip off the time value from Blad1 (in which case it will default to 12;00 AM) or create the calendar in a different manner (I would lean heavily towards the first option).
All of the above being said, I am frankly stumped as to why the code is not working. I can get it working by creating a separate measure for the previous day value, and then a 3rd measure that is the difference between the today measure and previous day measure. But I can't get the simple subtraction working inside a single measure with variables. I will need to do some more digging.
- Anonymous7 years agoNot applicable
Thank you so much for the time and effort you put into this,
I will check this out!
Thanks and merry christmas!
- Anonymous7 years agoNot applicable
Hi again, for some reason, some of the value-series can't be selected. Eventough they are very similiar to the other values. I just get a blank screen