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
o59393
Post Prodigy
Post Prodigy

Dax measure to get sum of highest volume in 2 consecutive months

Hi all

 

I have a dax measure that should return me the sum of 2 highest volume in consecutive months.

 

The table as example is the following:

 

o59393_0-1685336817071.png

 

As seen above, the dax should first the max demand (March in this case) and its predecessor (February). 

 

First dax I have is the one for the max demand:

 

 

 

VAR _A =

MAXX(
    VALUES(Query1[Period[Month 445]]]),
    Query1[Rolling with % produced by plant]
    )
    

 

 

 

It returns correctly:

 

o59393_1-1685337043680.png

 

Then I calculate the maximum month (which should be March in this example):

 

 

 

Validation Max Month = 


VAR _B =

CALCULATE (
        MAX ( Query1[Period[Month 445]]] ), 
        FILTER ('Query1', [Rolling with % produced by plant] = MAXX(VALUES(Query1[Period[Month 445]]]),Query1[Rolling with % produced by plant])))
    
        return _B

 

 

Is returning December which is incorrect (THIS IS THE DAX THAT NEEDS TO BE FIXED):

 

o59393_2-1685337159401.png

 

Then I calculate the predecessor month:

 

 

 

VAR _b =

CALCULATE (
        MAX ( Query1[Period[Month 445]]] ), 
        FILTER ('Query1', [Rolling with % produced by plant] = MAXX(VALUES(Query1[Period[Month 445]]]),Query1[Rolling with % produced by plant])))

VAR _D =
    DATE ( YEAR ( _B ), MONTH ( _B )-1, DAY ( _B ) )

RETURN _D

 

 

 

o59393_4-1685337335107.png

In this case returns November, which is incorrect (should be february)

 

The following dax is the total year demand:

 

o59393_3-1685337238974.png

Finally I calculate the 2 consecutive months:

 

 

 

2 Month Max demand = 

Var _a = 

MAXX(
    VALUES(Query1[Period[Month 445]]]),
    Query1[Rolling with % produced by plant]
    )

VAR _b =

CALCULATE (
        MAX ( Query1[Period[Month 445]]] ), 
        FILTER ('Query1', [Rolling with % produced by plant] = MAXX(VALUES(Query1[Period[Month 445]]]),Query1[Rolling with % produced by plant])))

Var _c = [Rolling with % produced by plant]

VAR _D =
    DATE ( YEAR ( _B ), MONTH ( _B )-1, DAY ( _B ) )

RETURN
_a + CALCULATE ( [Rolling with % produced by plant] , Query1[Period[Month 445]]] = _D )

 

 

o59393_5-1685337573032.png

 

As seen the result it return is not correct, the dax above is summing March (3.6MM) and November (2.4)

 

It should be march (3.6) and feb (2.6).

 

How can I solve it?

 

1 ACCEPTED SOLUTION

Hi,

Thank you for your message.

Please check the below picture and the attached pbix file.

The below measure is one of many ways to achieve it.

Jihwan_Kim_0-1685383963010.png

 

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


View solution in original post

4 REPLIES 4
Jihwan_Kim
Super User
Super User

Hi,

I am not sure if I understood your question correctly, but please chech the below picture and the attached pbix file.

 

Jihwan_Kim_0-1685338878548.png

 

Expected result measure: =
VAR _topvalue =
    CALCULATE (
        SUM ( Data[KO Volume] ),
        KEEPFILTERS (
            WINDOW (
                1,
                ABS,
                1,
                ABS,
                ALL ( Data[Month] ),
                ORDERBY ( CALCULATE ( SUM ( Data[KO Volume] ) ), DESC )
            )
        )
    )
VAR _prevmonthend =
    EOMONTH (
        FILTER (
            ALL ( Data[Month] ),
            CALCULATE ( SUM ( Data[KO Volume] ) ) = _topvalue
        ),
        -1
    )
VAR _prevmonthvalue =
    CALCULATE (
        SUM ( Data[KO Volume] ),
        EOMONTH ( Data[Month], 0 ) = _prevmonthend
    )
RETURN
    _topvalue + _prevmonthvalue

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


Hi @Jihwan_Kim 

 

Sorry if I wasn't clear. Let me put it easier.

 

How can I return in a dax measure the month whose got the highest volume in any month?

 

In this case the highest volume is march, therefore the dax should return me March:

 

o59393_0-1685371928320.png

 

I already have calculated the maximum volume for march:

 

o59393_1-1685373020432.png

 

How could I get the march month with dax?

 

Thanks.

 

 

 

Hi,

Thank you for your message.

Please check the below picture and the attached pbix file.

The below measure is one of many ways to achieve it.

Jihwan_Kim_0-1685383963010.png

 

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


You solved my problem @Jihwan_Kim 

 

Thank you!

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.