Forum Discussion

LRSFC_DanJ's avatar
LRSFC_DanJ
Regular Visitor
3 years ago
Solved

Using SWITCH with CALCULATE?

I'm new to Power BI so I apologise in advance if there's something obvious that I've missed.

 

I have a couple of measures which sum values within a specific date range, to allow comparing between two date ranges on the same column chart.

 

The original measure code I had was:

 

 

HH17 2018-2019 = CALCULATE(SUM('HH Consumption Report'[Consumption(KWh) HH17]),'HH Consumption Report'[Date] >= DATE(2018,9,1) && 'HH Consumption Report'[Date] <= DATE(2019,9,1))

 

 

This worked great as far as it went, but now I want to generalise it so that the column being looked at can be selected dynamically using a table (because I couldn't find anything that does a dropdown list box).

 

Trying to put the necessary SWITCH function call inside the SUM did not work, so what I'm trying now is I've unpivoted the data (so instead of columns named "Consumption(KWh) HH17" and so on, these are now values in a column called "Attribute") but I still don't seem to be able to use the new Attribute column together with a SWITCH function call in the CALCULATE conditions.

 

I've been trying the code below, but Power BI refuses to accept it, giving an error "A function 'PLACEHOLDER' has been used in a True/False expression that is used as a table filter expression. This is not allowed".

 

 

 

First HH 2018-2019 = CALCULATE(SUM('HH Consumption Report'[Value]),'HH Consumption Report'[Date] >= DATE(2018,9,1) && 'HH Consumption Report'[Date] <= DATE(2019,9,1) && 'HH Consumption Report'[Attribute] = SWITCH('HH Consumption Report'[Selected Measure],1,"Consumption(KWh) HH1",3,"Consumption(KWh) HH3",5,"Consumption(KWh) HH5",7,"Consumption(KWh) HH7",9,"Consumption(KWh) HH9",11,"Consumption(KWh) HH11",13,"Consumption(KWh) HH13",15,"Consumption(KWh) HH15",17,"Consumption(KWh) HH17",19,"Consumption(KWh) HH19",21,"Consumption(KWh) HH21",23,"Consumption(KWh) HH23",25,"Consumption(KWh) HH25",27,"Consumption(KWh) HH27",29,"Consumption(KWh) HH29",31,"Consumption(KWh) HH31",33,"Consumption(KWh) HH33",35,"Consumption(KWh) HH35",37,"Consumption(KWh) HH37",39,"Consumption(KWh) HH39",41,"Consumption(KWh) HH41",43,"Consumption(KWh) HH43",45,"Consumption(KWh) HH45",47,"Consumption(KWh) HH47"))

 

 

 

Can anyone suggest what I'm doing wrong?

 

Here is the code for [Selected Measure]:

 

 

 

Selected Measure = SELECTEDVALUE('Measure Selection'[Code],1)

 

 

 

And 'Measure Selection' is a table that looks like this:

 

HourCode
00:00 - 01:001
01:00 - 02:003
02:00 - 03:005
03:00 - 04:007
04:00 - 05:009
05:00 - 06:0011
06:00 - 07:0013
07:00 - 08:0015
08:00 - 09:0017
09:00 - 10:0019
10:00 - 11:0021
11:00 - 12:0023
12:00 - 13:0025
13:00 - 14:0027
14:00 - 15:0029
15:00 - 16:0031
16:00 - 17:0033
17:00 - 18:0035
18:00 - 19:0037
19:00 - 20:0039
20:00 - 21:0041
21:00 - 22:0043
22:00 - 23:0045
23:00 - 00:0047

 

This was based on ideas from this article: https://radacad.com/change-the-column-or-measure-value-in-a-power-bi-visual-by-selection-of-the-slicer-parameter-table-pattern

  • I've cracked it. The solution was to add Fiscal year to the "Legend" of the clustered column chart. This then has the result I wanted.

     

     

5 Replies