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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
NasTr
Frequent Visitor

Summarize and calculate difference using an index

Hi all, As you understand I am a newbie in dax and power query!

Before posting I have searched and tryied lots of things but wasn't able to find the solution.

I have a table that has lots of columns ( var1, var2, cyindex, lyindex,month etc) which contain not unique values.

I am trying to summarize 'TTL' by CYindex and by var1 and then calculate the difference where LYindex is CYIndex, Something like the defference from previous that we have in pivots, only now with continuous data, no dates and previous being defined by matching index... Any help?

sample data:

Var1CY indexLY IndexmonthVar2TTL
a20191120191011A18
a20191120191011G10
a20191120191011I76
a20191220191112A15
a20191220191112I76
a2020022020012I36
b20191020190910I397
b20191120191011A77
b20191120191011I873
b20191220191112A262
b20191220191112G398
b20191220191112I1940
b2020012019121I64
b2020022020012I348
b2020032020023A295
b2020032020023G226
b2020032020023I430
c20191020190910A27
c20191020190910I378
c20191120191011A313
c20191120191011G2556
c20191120191011I1885
c20191220191112A342
c20191220191112G1413
c20191220191112I1914
c2020012019121A96
c2020012019121I242
c2020022020012A110
c2020022020012G17
c2020022020012I454
c2020032020023A257
c2020032020023I307
d20191020190910I21
d20191120191011A99
d20191120191011I129
d20191220191112A48
d20191220191112G239
d20191220191112I122
d2020012019121A15
d2020012019121I51
d2020022020012I260
d2020032020023A29
d2020032020023G246
d2020032020023I432

 

desired result 1: 

Var1CY indexLY IndexSum of TTLLY ttl
b201910201909397 
b201911201910950397
b2019122019112600950
b202001201912642600
b20200220200134864
b202003202002951348

desired result 2:

var1Sum of TTL Sum of LY ttl
b53104359

 

 

Thank you

1 ACCEPTED SOLUTION
Icey
Community Support
Community Support

Hi @NasTr ,

 

Try this:

 

1. Create a calculated column.

When the data type of [CY index] column is number, try this:

CY Date = DATE ( LEFT ( [CY index], 4 ), RIGHT ( [CY index], 2 ), 1 )

When the data type of [CY index] column is text, try this:

CY Date = DATE ( CONVERT ( LEFT ( [CY index], 4 ), INTEGER ), CONVERT ( RIGHT ( [CY index], 2 ), INTEGER ), 1 )

 

2. Create a measure.

LY TTL =
VAR t =
    ADDCOLUMNS (
        'Table',
        "LY_TTL",
            CALCULATE (
                SUM ( 'Table'[TTL] ),
                ALLEXCEPT ( 'Table', 'Table'[Var1] ),
                PREVIOUSMONTH ( 'Table'[CY Date] )
            )
    )
VAR t2 =
    SUMMARIZE ( t, [Var1], [CY index], [LY_TTL] )
RETURN
    SUMX ( t2, [LY_TTL] )

previous.gif

 

Best Regards,

Icey

 

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

3 REPLIES 3
Icey
Community Support
Community Support

Hi @NasTr ,

 

Try this:

 

1. Create a calculated column.

When the data type of [CY index] column is number, try this:

CY Date = DATE ( LEFT ( [CY index], 4 ), RIGHT ( [CY index], 2 ), 1 )

When the data type of [CY index] column is text, try this:

CY Date = DATE ( CONVERT ( LEFT ( [CY index], 4 ), INTEGER ), CONVERT ( RIGHT ( [CY index], 2 ), INTEGER ), 1 )

 

2. Create a measure.

LY TTL =
VAR t =
    ADDCOLUMNS (
        'Table',
        "LY_TTL",
            CALCULATE (
                SUM ( 'Table'[TTL] ),
                ALLEXCEPT ( 'Table', 'Table'[Var1] ),
                PREVIOUSMONTH ( 'Table'[CY Date] )
            )
    )
VAR t2 =
    SUMMARIZE ( t, [Var1], [CY index], [LY_TTL] )
RETURN
    SUMX ( t2, [LY_TTL] )

previous.gif

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

NasTr
Frequent Visitor

Hi, @CNENFRNL thank you for your quick response. 

Unfortunately that didn't do the trick as date and var1 is not unique, but more important date is not continuous. So I might have var1 for 01/08/2019 but since I am missing 01/07/2019 it returns wrong value. 

Can I use something like max of list of earlier dates...and of course how! 

Also apart from var1 I have also other variables that I need the measure to react the same.

CNENFRNL
Community Champion
Community Champion

Hi, @NasTr , DAX does the trick without sweat, much easier than PQ, I think.

Screenshot 2020-10-14 113940.png

You might want to refer to the attached file for details.


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

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

Top Solution Authors
Top Kudoed Authors