Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

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.

Reply
LRSFC_DanJ
Regular Visitor

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-slic...

1 ACCEPTED SOLUTION
LRSFC_DanJ
Regular Visitor

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.

 

LRSFC_DanJ_0-1664445001027.png

 

View solution in original post

5 REPLIES 5
LRSFC_DanJ
Regular Visitor

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.

 

LRSFC_DanJ_0-1664445001027.png

 

LRSFC_DanJ
Regular Visitor

Following advice at https://community.powerbi.com/t5/Desktop/DAX-Formulas-for-Fiscal-Year-Fiscal-Quarter-and-Month-Short... I was able to create columns for Month, Year and Fiscal Year as follows:

 

Month = MONTH('HH Consumption Report 2018-2022'[Date])
Year = YEAR('HH Consumption Report 2018-2022'[Date])
Fiscal Year = IF('HH Consumption Report 2018-2022'[Month] >= 9, CONVERT('HH Consumption Report 2018-2022'[Year], STRING), CONVERT('HH Consumption Report 2018-2022'[Year] - 1, STRING))

 

Adding another slicer now allows me to select a single fiscal year to display. However I can't figure out how to make it show two fiscal years side by side - selecting multiples in the slicer just displays them added together.

 

LRSFC_DanJ_0-1664441791100.png

 

Any idea how I can have multiple fiscal years side by side?

 

LRSFC_DanJ
Regular Visitor

I was able to make some progress, I created calculated measures which added together the two half-hours which represent each hour, and was able to use these in a field parameter, which produced the following:

 

LRSFC_DanJ_0-1664439217458.png

 

However, I want to be able to have bars for different years side-by-side, ideally selectable using another slicer. And these are not calendar years but financial/academic years, i.e. they run from September to August. Not sure how I can accomplish this?

LRSFC_DanJ
Regular Visitor

@amitchandak Field parameters sounds like what I wanted, but I don't seem to be able to make it work with SUM?

 

I tried to do this:

 

 

Selected 2018-2019 = CALCULATE(SUM('Selected half-hours to compare'[Selected half-hours to compare]), 'HH Consumption Report 2018-2022'[Date] >= DATE(2018,9,1) && 'HH Consumption Report 2018-2022'[Date] <= DATE(2019,9,1))

 

But it does not work, it reports an error saying that SUM cannot work with a string.

("Selected half-hours to compare" is a Field parameter)

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors