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
ChrisFromOhio
Helper I
Helper I

Calculated Measure has incorrect grand total

Hello - I want to calculate YOY sales for various companies, but with different end dates. For example:

 

I have Company A sales data from 1/1/22 - 5/31/22, so prior year sales should be from 1/1/21 - 5/31/21.

I have Company B sales data from 1/1/22 - 6/15/22, so prior year sales should be from 1/1/21 - 6/15/21.

I have Company C sales data from 1/1/22 - 5/31/22, so prior year sales should be from 1/1/21 - 5/31/21.

 

I have sales data for companies A, B, and C from 1/1/21 - the dates listed above all in one combined query.

 

In order to prevent PY sales for companies A and C being calculated for 1/1/21 - 6/15/21, the formula I am using is as follows

 

 

IF(
    'SALES'[Sales $ Sum]<>BLANK(),
    IF(
        HASONEVALUE('Calendar'[Day]),
        SUMX(VALUES('SALES'[RETAILER]),
        CALCULATE(
            'SALES'[Sales $ Sum],
            SAMEPERIODLASTYEAR('Calendar'[Date])
        )),
        SUMX(VALUES('SALES'[RETAILER]),
        CALCULATE(
            'SALES'[Sales $ Sum],
            DATESBETWEEN(
                'Calendar'[Date],
                MIN('Calendar'[Date])-365,
                MAX('SALES'[Date])-365
            )
        )
    )
)
)

 

 

This works to get the total PY sales by retailer, accounting for retailer B having more days of sales, but in the grand total it is summing up to is taking all sales 1/1/21 - 6/15/21, when it should be leaving out sales 6/1/21 - 6/15/21 for retailers A and C. Any idea how to correct this?

1 ACCEPTED SOLUTION
ChrisFromOhio
Helper I
Helper I

After playing with this issue, I ended up creating a new measure that calculates the max date for each retailer, and then replacing that in the bottom half of my formula. The measure now reads 

IF(
    'SALES'[Sales $ Sum]<>BLANK(),
    IF(
        HASONEVALUE('Calendar'[Day]),
        SUMX(VALUES('SALES'[RETAILER]),
        CALCULATE(
            'SALES'[Sales $ Sum],
            SAMEPERIODLASTYEAR('Calendar'[Date])
        )),
        SUMX(VALUES('SALES'[RETAILER]),
        CALCULATE(
            'SALES'[Sales $ Sum],
            DATESBETWEEN(
                'Calendar'[Date],
                MIN('Calendar'[Date])-365,
                'SALES'[Sales Data Thru]-365
            )
        )
    )
)
)

View solution in original post

1 REPLY 1
ChrisFromOhio
Helper I
Helper I

After playing with this issue, I ended up creating a new measure that calculates the max date for each retailer, and then replacing that in the bottom half of my formula. The measure now reads 

IF(
    'SALES'[Sales $ Sum]<>BLANK(),
    IF(
        HASONEVALUE('Calendar'[Day]),
        SUMX(VALUES('SALES'[RETAILER]),
        CALCULATE(
            'SALES'[Sales $ Sum],
            SAMEPERIODLASTYEAR('Calendar'[Date])
        )),
        SUMX(VALUES('SALES'[RETAILER]),
        CALCULATE(
            'SALES'[Sales $ Sum],
            DATESBETWEEN(
                'Calendar'[Date],
                MIN('Calendar'[Date])-365,
                'SALES'[Sales Data Thru]-365
            )
        )
    )
)
)

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.