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
ashwinkolte
Helper III
Helper III

Need help with month over month calculation

ItemSubitem DateCost
Item1Subitem130-Jun-23100
Item1Subitem230-Jun-23200
Item1Subitem131-May-23400
Item1Subitem231-May-23600
Item2Subitem131-May-23500

 

I have inoput data given above . Task is to calculate cost saving i.e June month - May month for the items and its sub items

 

My challenge is this : The input data does not have data for June month for item2 . But it has data for May . Since there is data for May , I want June cost to be considered 0 and cost  saving over May calculated as (0-500). The overall output should be as below in a matrix form

 

ItemSubitem30-Jan-2331-May-23Month over month cost saving
Item1Subitem1                100           400-300
Item1Subitem2                200           600-400
Item2Subitem1                   -             500-500
total                 300        1,500-1200

 

In other words if current moth is june and there is not record for june but records are there for may then June should be considered 0 

 

3 ACCEPTED SOLUTIONS
Anonymous
Not applicable

Hi @ashwinkolte 

You can refer to the following example.

1.Create two tables

Table 2 = SUMMARIZE(ALLSELECTED('Table'),[Item],[Subitem ])
Table 3 = SUMMARIZE('Table',[Date])

2.Create two measures

Sum_cost =
VAR a =
    FILTER (
        'Table',
        [Item]
            IN VALUES ( 'Table 2'[Item] )
                && [Subitem ]
                    IN VALUES ( 'Table 2'[Subitem ] )
                        && EOMONTH ( [Date], 0 ) = EOMONTH ( SELECTEDVALUE ( 'Table 3'[Date] ), 0 )
    )
RETURN
    IF ( COUNTROWS ( a ) > 0, CALCULATE ( SUM ( 'Table'[Cost] ), a ), 0 )
over month cost saving =
[Sum_cost]
    - CALCULATE (
        SUM ( 'Table'[Cost] ),
        EOMONTH ( 'Table'[Date], 0 ) = EOMONTH ( SELECTEDVALUE ( 'Table 3'[Date] ), -1 ),
        'Table'[Item] IN VALUES ( 'Table 2'[Item] ),
        'Table'[Subitem ] IN VALUES ( 'Table 2'[Subitem ] )
    )

3.Put the field of table 2 to the row  ,  the field of table3 to column, the two measures to value in matrix visual.

Output

vxinruzhumsft_0-1693363950130.png

 

Best Regards!

Yolo Zhu

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

Anonymous
Not applicable

Hi @ashwinkolte 

You can create a new table

Table 2 =
VAR a =
    SUMMARIZE (
        ADDCOLUMNS (
            CALENDAR ( DATE ( 2022, 1, 1 ), DATE ( 2023, 12, 31 ) ),
            "Eomonth", EOMONTH ( [Date], 0 )
        ),
        [Eomonth]
    )
VAR b =
    SUMMARIZE ( 'Table', [Product], [category] )
VAR c =
    ADDCOLUMNS (
        GENERATE ( a, b ),
        "flag",
            VAR _count =
                COUNTROWS (
                    FILTER (
                        'Table',
                        [Product] = EARLIER ( 'Table'[Product] )
                            && [category] = EARLIER ( 'Table'[category] )
                            && YEAR ( [Date] ) = YEAR ( [Eomonth] )
                    )
                )
            RETURN
                IF ( _count > 0, 1, 0 )
    )
RETURN
    ADDCOLUMNS (
        SUMMARIZE (
            FILTER ( c, [flag] = 1 ),
            'Table'[Product],
            'Table'[category],
            [Eomonth]
        ),
        "Sales",
            VAR _lookup =
                MAXX (
                    FILTER (
                        'Table',
                        [Product] = EARLIER ( 'Table'[Product] )
                            && [category] = EARLIER ( 'Table'[category] )
                            && EOMONTH ( [Date], 0 ) = EARLIER ( [Eomonth] )
                    ),
                    [Sales]
                )
            RETURN
                IF ( _lookup <> BLANK (), _lookup, 0 )
    )

Output

vxinruzhumsft_0-1693384128467.png

 

Best Regards!

Yolo Zhu

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

ashwinkolte
Helper III
Helper III

Absolutely Brilliant ! Thanks a lot @Anonymous 

View solution in original post

4 REPLIES 4
ashwinkolte
Helper III
Helper III

Absolutely Brilliant ! Thanks a lot @Anonymous 

Anonymous
Not applicable

Hi @ashwinkolte 

You can refer to the following example.

1.Create two tables

Table 2 = SUMMARIZE(ALLSELECTED('Table'),[Item],[Subitem ])
Table 3 = SUMMARIZE('Table',[Date])

2.Create two measures

Sum_cost =
VAR a =
    FILTER (
        'Table',
        [Item]
            IN VALUES ( 'Table 2'[Item] )
                && [Subitem ]
                    IN VALUES ( 'Table 2'[Subitem ] )
                        && EOMONTH ( [Date], 0 ) = EOMONTH ( SELECTEDVALUE ( 'Table 3'[Date] ), 0 )
    )
RETURN
    IF ( COUNTROWS ( a ) > 0, CALCULATE ( SUM ( 'Table'[Cost] ), a ), 0 )
over month cost saving =
[Sum_cost]
    - CALCULATE (
        SUM ( 'Table'[Cost] ),
        EOMONTH ( 'Table'[Date], 0 ) = EOMONTH ( SELECTEDVALUE ( 'Table 3'[Date] ), -1 ),
        'Table'[Item] IN VALUES ( 'Table 2'[Item] ),
        'Table'[Subitem ] IN VALUES ( 'Table 2'[Subitem ] )
    )

3.Put the field of table 2 to the row  ,  the field of table3 to column, the two measures to value in matrix visual.

Output

vxinruzhumsft_0-1693363950130.png

 

Best Regards!

Yolo Zhu

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

 

Dear @Anonymous 

 

Thankyou so much ! It worked . Really appreciate it . Thanks a lot. May I take the liberty to ask for more help if you dont mind 🙂 

 

I have a input table as below

 

ProductcategorySalesDate
P1C110031-Jan-23
P1 C220030-Apr-23
P2C1100

30-Jun-22

 

I want an output table as given below. i.e for whichever months OF THE YEAR there are no sales it should insert row with 0 sales for EACH product category combination .

 

ProductcategorySalesDate
P1C110031-Jan-23
P1C1028-Feb-23
P1C1031-Mar-23
P1C1030-Apr-23
P1C1031-May-23
P1C1030-Jun-23
P1C1031-Jul-23
P1C1031-Aug-23
P1C1030-Sep-23
P1C1031-Oct-23
P1C1030-Nov-23
P1C1031-Dec-23
P1C2031-Jan-23
P1C2028-Feb-23
P1C2031-Mar-23
P1C220030-Apr-23
P1C2031-May-23
P1C2030-Jun-23
P1C2031-Jul-23
P1C2031-Aug-23
P1C2030-Sep-23
P1C2031-Oct-23
P1C2030-Nov-23
P1C2031-Dec-23
P2C1031-Jan-22
P2C1028-Feb-22
P2C1031-Mar-22
P2C1030-Apr-22
P2C1031-May-22
P2C110030-Jun-22
P2C1031-Jul-22
P2C1031-Aug-22
P2C1030-Sep-22
P2C1031-Oct-22
P2C1030-Nov-22
P2C1031-Dec-22

 

Appreciate if you can help with this 

Anonymous
Not applicable

Hi @ashwinkolte 

You can create a new table

Table 2 =
VAR a =
    SUMMARIZE (
        ADDCOLUMNS (
            CALENDAR ( DATE ( 2022, 1, 1 ), DATE ( 2023, 12, 31 ) ),
            "Eomonth", EOMONTH ( [Date], 0 )
        ),
        [Eomonth]
    )
VAR b =
    SUMMARIZE ( 'Table', [Product], [category] )
VAR c =
    ADDCOLUMNS (
        GENERATE ( a, b ),
        "flag",
            VAR _count =
                COUNTROWS (
                    FILTER (
                        'Table',
                        [Product] = EARLIER ( 'Table'[Product] )
                            && [category] = EARLIER ( 'Table'[category] )
                            && YEAR ( [Date] ) = YEAR ( [Eomonth] )
                    )
                )
            RETURN
                IF ( _count > 0, 1, 0 )
    )
RETURN
    ADDCOLUMNS (
        SUMMARIZE (
            FILTER ( c, [flag] = 1 ),
            'Table'[Product],
            'Table'[category],
            [Eomonth]
        ),
        "Sales",
            VAR _lookup =
                MAXX (
                    FILTER (
                        'Table',
                        [Product] = EARLIER ( 'Table'[Product] )
                            && [category] = EARLIER ( 'Table'[category] )
                            && EOMONTH ( [Date], 0 ) = EARLIER ( [Eomonth] )
                    ),
                    [Sales]
                )
            RETURN
                IF ( _lookup <> BLANK (), _lookup, 0 )
    )

Output

vxinruzhumsft_0-1693384128467.png

 

Best Regards!

Yolo Zhu

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

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.