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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
holodan95
Helper II
Helper II

Values from previous and current month, but with one month selected in slicer

Hi,

 

Can you guys and gals tell me how to calculate with values both from current and previous month, while a single month is selected in a slicer?

I want to calculate the power usage of my client, for which I need their kwh position from the previous and current month.

 

My measure works by subtracting current month's last value from previous month's last value like:

Kwh position on 31/nov/2022 = 1200

Kwh position on 31/oct/2022 = 1000

Return value for 11th month is 200kwh. 

 

This measure is then built into another one with summarize in it, so I'm able to sumx all the values, to return a TOTAL like:

Measure =

Var _1 = SUMMARIZE(POWER_DATA,CLIENT_ID,MONTH,"usage",[usage_measure])

Return

Sumx(_1,[usage])

 

The problem starts when I only select one month in a slicer, because then my measure will return a blank value, as it cannot calculate with prev. Month's values. 

 

My first measure:

usage_measure =


var MAX_month_previous = CALCULATE(
MAX(Power_data[KWH entry]),
FILTER(ALLSELECTED(Power_data),
Power_data[Rank_Month]=SELECTEDVALUE(Power_data[Rank_Month])-1 && Power_data[Client_ID]=SELECTEDVALUE(Power_data[Client_ID]))) +0

 

var MAX_month_current = CALCULATE(
MAX(Power_data[KWH entry]),
FILTER(ALLSELECTED(Power_data),
Power_data[Rank_Month]=SELECTEDVALUE(Power_data[Rank_Month]) && Power_data[Client_ID]=SELECTEDVALUE(Power_data[Client_ID]))) +0
return
Max_month_current - Max_month_previous

 

 

Please help me with my project.

2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

Hi @holodan95 ,

Please try below steps:

1. create a measure with below dax formula

Usage_measure =
VAR cur_month =
    SELECTEDVALUE ( 'Power_data'[Month_of_entry] )
VAR cur_id =
    SELECTEDVALUE ( 'Power_data'[Client_ID] )
VAR tmp =
    FILTER (
        ALL ( 'Power_data' ),
        'Power_data'[Month_of_entry] = cur_month
            && 'Power_data'[Client_ID] = cur_id
    )
VAR tmp1 =
    FILTER (
        ALL ( 'Power_data' ),
        'Power_data'[Month_of_entry] = cur_month - 1
            && 'Power_data'[Client_ID] = cur_id
    )
VAR cur_max_date =
    MAXX ( tmp, [Day_of_entry] )
VAR pre_max_date =
    MAXX ( tmp1, [Day_of_entry] )
VAR val_cur_month =
    CALCULATE (
        MAX ( 'Power_data'[KWH entry] ),
        'Power_data'[Month_of_entry] = cur_month,
        'Power_data'[Day_of_entry] = cur_max_date,
        'Power_data'[Client_ID] = cur_id
    )
VAR val_pre_month =
    CALCULATE (
        MAX ( 'Power_data'[KWH entry] ),
        'Power_data'[Month_of_entry] = cur_month - 1,
        'Power_data'[Day_of_entry] = pre_max_date,
        'Power_data'[Client_ID] = cur_id
    )
VAR val = val_cur_month - val_pre_month
RETURN
    val

2. add a slicer with  'Power_data'[Month_of_entry], add a table visual with 'Power_data'[Client_ID] and measure

vbinbinyumsft_0-1672212724547.png

Please refer the attached .pbix file.

 

Best regards,
Community Support Team_Binbin Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

holodan95
Helper II
Helper II

Hi,

 

My working solution was to make a new table with groupby, using a variable summarize table.
I used table as a base for making the calculations in the matrix visual.

View solution in original post

6 REPLIES 6
holodan95
Helper II
Helper II

Hi,

 

My working solution was to make a new table with groupby, using a variable summarize table.
I used table as a base for making the calculations in the matrix visual.

Anonymous
Not applicable

Hi @holodan95 ,

Please try below steps:

1. create a measure with below dax formula

Usage_measure =
VAR cur_month =
    SELECTEDVALUE ( 'Power_data'[Month_of_entry] )
VAR cur_id =
    SELECTEDVALUE ( 'Power_data'[Client_ID] )
VAR tmp =
    FILTER (
        ALL ( 'Power_data' ),
        'Power_data'[Month_of_entry] = cur_month
            && 'Power_data'[Client_ID] = cur_id
    )
VAR tmp1 =
    FILTER (
        ALL ( 'Power_data' ),
        'Power_data'[Month_of_entry] = cur_month - 1
            && 'Power_data'[Client_ID] = cur_id
    )
VAR cur_max_date =
    MAXX ( tmp, [Day_of_entry] )
VAR pre_max_date =
    MAXX ( tmp1, [Day_of_entry] )
VAR val_cur_month =
    CALCULATE (
        MAX ( 'Power_data'[KWH entry] ),
        'Power_data'[Month_of_entry] = cur_month,
        'Power_data'[Day_of_entry] = cur_max_date,
        'Power_data'[Client_ID] = cur_id
    )
VAR val_pre_month =
    CALCULATE (
        MAX ( 'Power_data'[KWH entry] ),
        'Power_data'[Month_of_entry] = cur_month - 1,
        'Power_data'[Day_of_entry] = pre_max_date,
        'Power_data'[Client_ID] = cur_id
    )
VAR val = val_cur_month - val_pre_month
RETURN
    val

2. add a slicer with  'Power_data'[Month_of_entry], add a table visual with 'Power_data'[Client_ID] and measure

vbinbinyumsft_0-1672212724547.png

Please refer the attached .pbix file.

 

Best regards,
Community Support Team_Binbin Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Ashish_Mathur
Super User
Super User

Hi,

Share data in a format that can be pasted in an MS Excel file. 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi,

 

Please find below a sample data:


Client_IDDay_of_entryMonth_of_entryKWH entryRank_monthRank_day-month
CL12022.11.0511011
CL12022.11.101120412
CL12022.11.151132513
CL12022.11.201148814
CL12022.11.251170815
CL12022.11.301199216
CL12022.12.0512114421
CL12022.12.1012130822
CL12022.12.3112143423
CL22022.11.0111011
CL22022.11.091127412
CL22022.11.171169313
CL22022.11.2511112414
CL22022.12.0312148521
CL22022.12.1112193322
CL22022.12.1912229323
CL22022.12.2712270624
CL22023.01.041292931
CL22023.01.121334732

 

Let's say we have a month slicer, with december selected. In that case I need to return

442 for CL1 (1434-992) and 1582 for CL2 (2706-1124).

 

My original measure is the following, which successfully calculates these values, when there is no month selected:

 

Usage_measure =

var MAX_month_previous = CALCULATE(
    MAX(Power_data[KWH entry]),
    FILTER(ALLSELECTED(Power_data),
    Power_data[Rank_Month]=SELECTEDVALUE(Power_data[Rank_Month])-1 && Power_data[Client_ID]=SELECTEDVALUE(Power_data[Client_ID]))) +0

var MAX_month_current = CALCULATE(
MAX(Power_data[KWH entry]),
    FILTER(ALLSELECTED(Power_data),
    Power_data[Rank_Month]=SELECTEDVALUE(Power_data[Rank_Month]) && Power_data[Client_ID]=SELECTEDVALUE(Power_data[Client_ID]))) +0
return
Max_month_current - Max_month_previous

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi,

Thank you, but I can only work with my measure, as the sample data I gave is very simple, free of faulty entries, and factors that I need to calculate with.

In some cases I have to ignore the last value, and calculate with the one which is prior to that etc..

Is there a way to adapt my measure or simplify it a way, so it does the same as yours?
I'll try and adapt some of your DAX like previousmonth and it out. But at first glance, it is to simple to work with my dataset.

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.