Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Measures in Power BI Calculation

Hello All ,
This is a question which has been a problem for atleast a month . I am using Power BI for more than a year. But I am stuck here and any help will be highly appreciated
Following is the dummy datatable 

StateSaleIDSale DateTotal SaleService Period  (in months)
A11/1/202324913
A21/1/202231715
B31/1/202029120
B41/1/20213885
C55/2/20231099
C65/3/202229312

 

I have created a calendar table , which has all dates form 2019 to 2024. This date from calendar table is in one to many relationship with Sale Date 

 

Following is what is the desired result in power bi 

  2023 
 StateCurrent year SaleCarry forward Sale  
 A229.846263.4  
 B    
 C    



this is a simple presentation , i have only calculated value for 2023 , we do have to include months and other years 


Defination : 

Current year Sale -  To calculate this first we need sale per month i.e Sales / Service Period 
Important thing to keep in mind that current year sale means addition of sales/service period for the year it was sold (Sale date)
for example if sale date is 01/01/2022 then for that specific sale ID current year sale for all years will be 0 except 2022

example from dummy data for first row sale/service period = 19.15 
this for service period of 13 months 
but as the current year only can be for 12 months hence left out months after sale date is jan(As sale was done in jan) - dec = 12 
hence 19.15*12 = 229.8
And as ID 2  was not sold in 2023 , hence final Current year sale for A in 2023 will be 229.8+0 = 229.8
and the rest left will go for carry forward sales for the same saleID for next year. But remeber we only calculate carry forward for 2 years or left our service period after current year sale (whichever smaller)
Like for example from dummy data for ID 2 , it was sold in 2022 , hence that means its carry forward will be accounted in 2023 , 
hence for ID 2 , 
Sale/sercvice period = 317/15 = 21.13
for its carry foward period lets first find out current year period of same ID 
for ID 2 service period is 15 months and it was sold in january hence leaving (jan-dec) 12 months rest is carry forward period of 3 months  (here we used 3 as 3 months is smaller than 2 years, as mentioned above that for carry forward only take left out servic period after current year sale or 2 years whichever smaller )
hence 3*21.13 = 63.4
as there is no other ID which was sold in 2021 or 2022 for A 
Hence carry forward for A is 63.4





Please help 
dax 
GuyInACube 
amitchandak 
AbbasG 
technolog 

 

  • Hi Anonymous ,

     

    Please try:

    Carry forward Sale2 = 
    VAR _a =
        MAXX ( 'Calendar', [Date] )
    VAR _b =
        MINX ( 'Calendar', [Date] )
    VAR _c =
        SELECTCOLUMNS ( 'Table', "State", [State] )
    var _e = SELECTCOLUMNS('Table',"Date",[Sale Date])   
    VAR _d =
        SUMX (
            FILTER ( ALL ( 'Table' ), [State] IN _c && [Sale Date] <= _a &&[Sale Date] in _e),
            SWITCH (
                TRUE (),
                NOT([Sale Date] in SELECTCOLUMNS('Table',"Date",[Sale Date])),0,
                DATEDIFF ( [Sale Date], _a, YEAR ) >= 2, 0,
                DATEDIFF ( [Sale Date], _a, YEAR ) <= 0, 0,
                DATEDIFF ( [Sale Date], _b, MONTH ) <= [Service Period  (in months)],
                    DIVIDE ( [Total Sale], [Service Period  (in months)] )
                        * ( [Service Period  (in months)] - DATEDIFF ( [Sale Date], _b, MONTH ) ),
                0
            )
        )
    RETURN
        _d

    Final output:

    Best Regards,

    Jianbo Li

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

  • Anonymous's avatar
    Anonymous
    3 years ago

    I made some changes to   and this works perfectly

     

    Carry forward Avoidance =

    VAR _a =

        MAXX ( 'Calendar', [Date] )  

    VAR _b =

        MINX ( 'Calendar', [Date] )  

    VAR _c =

        SELECTCOLUMNS ( 'Data', "Catg", [Catg] )

    var _e = SELECTCOLUMNS('Data',"Date",[SaleDate])  

    VAR _d =

        SUMX (                          

            FILTER ( ALL ( 'Data' ), [Catg] IN _c && [SaleDate] <= _a && Data[SaleDate] in _e ),

            SWITCH (

                TRUE (),

                 NOT([Saledate] in SELECTCOLUMNS('Data',"Date",[Saledate])),0,

                DATEDIFF ( [Saledate], _a, YEAR ) = 1,

                    SWITCH (

                        TRUE (),    




                        (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ) < Data[ServiceMonths]

                        &&

                        (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ) + 12 >= Data[ServiceMonths]

                         &&

                                            Data[ServiceMonths] - (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ) <= 12

    ,

                        Data[ServiceMonths] - (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ),

                        (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ) < Data[ServiceMonths]

                        &&

                        (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ) + 12 >= Data[ServiceMonths]

                         &&

                                            Data[ServiceMonths] - (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ) >12

    ,

                        12,

                        (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1) + 12  < Data[ServiceMonths] , 12 ,

                        (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1) >= Data[ServiceMonths] , 0 ,0)

                    ,

                DATEDIFF ( [Saledate], _a, YEAR ) = 2,

                    SWITCH (

                        TRUE (),

     

                        (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1)+12  < Data[ServiceMonths]

                        &&

                        (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1)+12 + 12 <=  Data[ServiceMonths]

                        , 12,

                        (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1)+12  < Data[ServiceMonths]

                        &&

                        (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1)+12 + 12 >= Data[ServiceMonths]

     

                        , Data[ServiceMonths]-((DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1)+12 ),

                        (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1)>=Data[ServiceMonths],0,0),0)

                        * DIVIDE(Data[AvoidanceSale],Data[ServiceMonths]))

    return _d



     

     

    Current year Avoidance sale =

    VAR _a =

        MAXX ( 'Calendar', [Date] )

    VAR _b =

        SUMX (

            FILTER('Contracts',Contracts[Saledate]<=_a&&Contracts[Saledate]>=MINX('Calendar',[Date])),

            DIVIDE ( Contracts[AvoidanceSale], [Service Months] )

                * (

                    MINX (

                        { DATEDIFF ( 'Contracts'[Saledate], _a, MONTH ), [Service Months] },

                        [Value]

                    )

                        + IF (

                            MINX (

                                { DATEDIFF ( 'Contracts'[Saledate], _a, MONTH ), [Service Months] },

                                [Value]

                            ) = [Service Months],

                            0,

                            1

                        )

                )

        )

    RETURN

        _b

     

16 Replies

  • Hi Anonymous ,

     

    Please try:

    Current year Sale =
    VAR _a =
        MAXX ( 'Calendar', [Date] )
    VAR _b =
        SUMX (
            'Table',
            DIVIDE ( [Total Sale], [Service Period  (in months)] )
                * (
                    MINX (
                        { DATEDIFF ( 'Table'[Sale Date], _a, MONTH ), [Service Period  (in months)] },
                        [Value]
                    ) + 1
                )
        )
    RETURN
        _b
    
    
    
    
    Carry forward Sale =
    VAR _a =
        MAXX ( 'Calendar', [Date] )
    VAR _b =
        MINX ( 'Calendar', [Date] )
    VAR _c =
        SUMX (
            FILTER (
                ALL ( 'Table' ),
                [State] = SELECTEDVALUE ( 'Table'[State] )
                    && [Sale Date] <= _a
            ),
            SWITCH (
                TRUE (),
                DATEDIFF ( [Sale Date], _a, YEAR ) >= 2, 0,
                DATEDIFF ( [Sale Date], _a, YEAR ) <= 0, 0,
                DATEDIFF ( [Sale Date], _b, MONTH ) <= [Service Period  (in months)],
                    DIVIDE ( [Total Sale], [Service Period  (in months)] )
                        * ( [Service Period  (in months)] - DATEDIFF ( [Sale Date], _b, MONTH ) )
            )
        )
    RETURN
        _c
    

    Final output:

    Best Regards,

    Jianbo Li

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

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hello v-jianboli-msft 
      I have tried manual calculation to see which measure is wrong 

      This is measure that you helped me with 

       

       

      This is manual calculation (these numbers should match)

      for B state 
      2020 - Current Year - 174.6
      2020 - Carry forward - 0
      2021 - Current Year -388
      2021 - Carry Forward - 116.4


      Hence when I can see Current Year sale is wrong as number for 2021 doesnt match for B when tried with your measure and manually in excel

      • Anonymous's avatar
        Anonymous
        Not applicable

        v-jianboli-msft 
        I changed your measure of current year sale to followinf 

        Current year Sale =
        VAR _a = MAXX ( 'Calendar', [Date] )

        VAR _b = SUMX (
        'Table',
        DIVIDE ( [Total Sale], [Service Period (in months)] )
        * (
        MINX (
        { DATEDIFF ( 'Table'[Sale Date], _a, MONTH ), [Service Period (in months)] },
        [Value]
        ) + IF(MINX (
        { DATEDIFF ( 'Table'[Sale Date], _a, MONTH ), [Service Period (in months)] },
        [Value]
        ) = [Service Period (in months)], 0, 1)
        )
        )

        RETURN _b

        And now everything is perfect 

        One last help with same measure 


        Why is column level total not working for carry forward  😞

         



  • Hi Anonymous ,

     

    Please try:

    Remove the relationship between Calendar and Table

    Then apply the measrue:

    Carry forward Sale = 
    VAR _a =
        MAXX ( 'Calendar', [Date] )
    VAR _b =
        MINX ( 'Calendar', [Date] )
    VAR _c =
        SELECTCOLUMNS ( 'Table', "State", [State] )
    VAR _d =
        SUMX (
            FILTER ( ALL ( 'Table' ), [State] IN _c && [Sale Date] <= _a ),
            SWITCH (
                TRUE (),
                DATEDIFF ( [Sale Date], _a, YEAR ) >= 2, 0,
                DATEDIFF ( [Sale Date], _a, YEAR ) <= 0, 0,
                DATEDIFF ( [Sale Date], _b, MONTH ) <= [Service Period  (in months)],
                    DIVIDE ( [Total Sale], [Service Period  (in months)] )
                        * ( [Service Period  (in months)] - DATEDIFF ( [Sale Date], _b, MONTH ) ),
                0
            )
        )
    RETURN
        _d
    
    Current year Sale = 
    VAR _a =
        MAXX ( 'Calendar', [Date] )
    VAR _b =
        SUMX (
            FILTER('Table',[Sale Date]<=_a&&[Sale Date]>=MINX('Calendar',[Date])),
            DIVIDE ( [Total Sale], [Service Period  (in months)] )
                * (
                    MINX (
                        { DATEDIFF ( 'Table'[Sale Date], _a, MONTH ), [Service Period  (in months)] },
                        [Value]
                    )
                        + IF (
                            MINX (
                                { DATEDIFF ( 'Table'[Sale Date], _a, MONTH ), [Service Period  (in months)] },
                                [Value]
                            ) = [Service Period  (in months)],
                            0,
                            1
                        )
                )
        )
    RETURN
        _b
    

    Final output:

    Best Regards,

    Jianbo Li

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

  • Hi Anonymous ,

     

    Please try:

    Carry forward Sale = 
    VAR _a =
        MAXX ( 'Calendar', [Date] )
    VAR _b =
        MINX ( 'Calendar', [Date] )
    VAR _c =
        SELECTCOLUMNS ( 'Table', "State", [State] )
    VAR _d =
        SUMX (
            FILTER ( ALL ( 'Table' ), [State] IN _c && [Sale Date] <= _a ),
            SWITCH (
                TRUE (),
                DATEDIFF ( [Sale Date], _a, YEAR ) = 1,
                    SWITCH (
                        TRUE (),
                        [Service Period  (in months)]
                            - DATEDIFF ( [Sale Date], DATE ( YEAR ( [Sale Date] ), 12, 31 ), MONTH ) - 1 > 12, 12,
                        [Service Period  (in months)]
                            - DATEDIFF ( [Sale Date], DATE ( YEAR ( [Sale Date] ), 12, 31 ), MONTH ) - 1 <= 12
                            && [Service Period  (in months)]
                                - DATEDIFF ( [Sale Date], DATE ( YEAR ( [Sale Date] ), 12, 31 ), MONTH ) - 1 > 0,
                            [Service Period  (in months)]
                                - DATEDIFF ( [Sale Date], DATE ( YEAR ( [Sale Date] ), 12, 31 ), MONTH ) - 1,
                        0
                    ),
                DATEDIFF ( [Sale Date], _a, YEAR ) = 2,
                    SWITCH (
                        TRUE (),
                        [Service Period  (in months)]
                            - DATEDIFF ( [Sale Date], DATE ( YEAR ( [Sale Date] ) + 1, 12, 31 ), MONTH ) - 1 >= 12, 12,
                        [Service Period  (in months)]
                            - DATEDIFF ( [Sale Date], DATE ( YEAR ( [Sale Date] ) + 1, 12, 31 ), MONTH ) - 1 < 12
                            && [Service Period  (in months)]
                                - DATEDIFF ( [Sale Date], DATE ( YEAR ( [Sale Date] ), 12, 31 ), MONTH ) - 1 > 0,
                            [Service Period  (in months)]
                                - DATEDIFF ( [Sale Date], DATE ( YEAR ( [Sale Date] ) + 1, 12, 31 ), MONTH ) - 1,
                        0
                    ),
                0
            )
                * DIVIDE ( [Total Sale], [Service Period  (in months)] )
        )
    RETURN
        _d

    Final output:

    Best Regards,

    Jianbo Li

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

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks v-jianboli-msft  This is wonderful 
      But i still have one last problem filtering 
      When i filter using Sale date , still the carry forward doesnt change it keeps calculating irresepective of filter 

       


      What should i change in the measure of carry forward which will make me use sale date as filter and help me calculate carry forward of only Sale ID's which were sold in selected dates in the filter 

      • Anonymous's avatar
        Anonymous
        Not applicable

        Please help v-jianboli-msft  this is the last thing that is missing that is filtering using sale date for both carry forward and current year sale 

        other than that the answers are correct please after this I will accept the solution 

        I really appreciate your help v-jianboli-msft 

        Please help

  • Anonymous's avatar
    Anonymous
    Not applicable

    I made some changes to   and this works perfectly

     

    Carry forward Avoidance =

    VAR _a =

        MAXX ( 'Calendar', [Date] )  

    VAR _b =

        MINX ( 'Calendar', [Date] )  

    VAR _c =

        SELECTCOLUMNS ( 'Data', "Catg", [Catg] )

    var _e = SELECTCOLUMNS('Data',"Date",[SaleDate])  

    VAR _d =

        SUMX (                          

            FILTER ( ALL ( 'Data' ), [Catg] IN _c && [SaleDate] <= _a && Data[SaleDate] in _e ),

            SWITCH (

                TRUE (),

                 NOT([Saledate] in SELECTCOLUMNS('Data',"Date",[Saledate])),0,

                DATEDIFF ( [Saledate], _a, YEAR ) = 1,

                    SWITCH (

                        TRUE (),    




                        (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ) < Data[ServiceMonths]

                        &&

                        (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ) + 12 >= Data[ServiceMonths]

                         &&

                                            Data[ServiceMonths] - (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ) <= 12

    ,

                        Data[ServiceMonths] - (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ),

                        (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ) < Data[ServiceMonths]

                        &&

                        (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ) + 12 >= Data[ServiceMonths]

                         &&

                                            Data[ServiceMonths] - (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ) >12

    ,

                        12,

                        (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1) + 12  < Data[ServiceMonths] , 12 ,

                        (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1) >= Data[ServiceMonths] , 0 ,0)

                    ,

                DATEDIFF ( [Saledate], _a, YEAR ) = 2,

                    SWITCH (

                        TRUE (),

     

                        (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1)+12  < Data[ServiceMonths]

                        &&

                        (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1)+12 + 12 <=  Data[ServiceMonths]

                        , 12,

                        (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1)+12  < Data[ServiceMonths]

                        &&

                        (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1)+12 + 12 >= Data[ServiceMonths]

     

                        , Data[ServiceMonths]-((DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1)+12 ),

                        (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1)>=Data[ServiceMonths],0,0),0)

                        * DIVIDE(Data[AvoidanceSale],Data[ServiceMonths]))

    return _d



     

     

    Current year Avoidance sale =

    VAR _a =

        MAXX ( 'Calendar', [Date] )

    VAR _b =

        SUMX (

            FILTER('Contracts',Contracts[Saledate]<=_a&&Contracts[Saledate]>=MINX('Calendar',[Date])),

            DIVIDE ( Contracts[AvoidanceSale], [Service Months] )

                * (

                    MINX (

                        { DATEDIFF ( 'Contracts'[Saledate], _a, MONTH ), [Service Months] },

                        [Value]

                    )

                        + IF (

                            MINX (

                                { DATEDIFF ( 'Contracts'[Saledate], _a, MONTH ), [Service Months] },

                                [Value]

                            ) = [Service Months],

                            0,

                            1

                        )

                )

        )

    RETURN

        _b

     

  • sneddon65's avatar
    sneddon65
    Frequent Visitor

    I'm wondering if anyone can help me with a MEASURE calculation within a tabular report in Power BI desktop.

     

    Here is the challenge. I have a tabular visual in PBI desktop producing trip data including Vehicle Registration, Start date and time and End date and time from some different tables in my sql database. I also have a final end odometer reading but I do not have the start odometer reading in each row of data. I need a MEASURE formula to lookup the previous date and time based on the Vehicle Registration so that i have a Start and End Odometer reading and then from that I calcxulate the journey length. Is this possible?

     

    The Vehicle table stores the Registration and another table stores the dates / times and the end odometer reading. The two tables are connected by a Vehicle Id.