Forum Discussion

NasTr's avatar
NasTr
Frequent Visitor
5 years ago
Solved

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

  • 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] )
    

     

    Best Regards,

    Icey

     

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

3 Replies

  • NasTr's avatar
    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.

  • Icey's avatar
    Icey
    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] )
    

     

    Best Regards,

    Icey

     

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