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
Bilal0336
Frequent Visitor

Consider sales of the last non blank quarter if the previous quarter is blank (not having any value)

I have two tables:

CALENDER_DIM: [ENDOFMONTH, YEAR, QUARTER, QUARTERNUM]

SALES_FACT: [ENDOFMONTH, PRODUCTID, SALE`S]

 

I want to show a comparison of the current quarter's sales with the previous quarter's sales, and I am using these measures:
Sales - This Quarter = SUM(SALES_FACT[SALES])

Sales - Previous Quarter (Having Sales) = CALCULATE([Sales - This Quarter],PREVIOUSQUARTER(CALENDAR_DIM[ENDOFMONTH]))

Currently, for the Sales - Previous Quarter column if the previous quarter is not having any sales, it is showing Blank:

Bilal0336_0-1674305836005.png



But what I want to achieve is that: if the previous quarter is blank, consider the last/previous non-blank quarter sales:
Expected output:

Bilal0336_1-1674306263032.png

 

So, for 2021, Q2 was null/blank, in that case for 2021 Q3 for the Sales - Previous Quarter column, instead of considering 2021 Q2, consider 2021 Q1.

Similarly, for 2022, Q1 is not having any sales and it is blank, so for 2022 Q2 for the Sales - Previous Quarter column, instead of considering 2022 Q1, consider 2021 Q4 (Which is having sales).

In short, if the previous quarter is blank, consider the latest previous quarter which is having any sales.


Any guidance or help will be appreciated, Thanks.
@amitchandak @tamerj1 @Greg_Deckler @GuyInACube 

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

I tried to create a sample pbix file like below.

I hope the below can provide some ideas on how to create a solution for your datamodel.

 

Jihwan_Kim_0-1674360441325.pngJihwan_Kim_1-1674361791511.png

 

Sales Prev Q nonblank: =
IF (
    HASONEVALUE ( 'Calendar'[Quarter] ),
    IF (
        [Sales this Q:] <> BLANK (),
        CALCULATE (
            [Sales this Q:],
            OFFSET (
                -1,
                FILTER (
                    ADDCOLUMNS (
                        ALL (
                            'Calendar'[Year],
                            'Calendar'[Quarter],
                            'Calendar'[End of quarter date CC]
                        ),
                        "@sales", [Sales this Q:] + 0
                    ),
                    [@sales] <> 0
                ),
                ORDERBY ( 'Calendar'[End of quarter date CC], ASC )
            )
        ),
        CALCULATE (
            [Sales this Q:],
            OFFSET (
                -1,
                ADDCOLUMNS (
                    ALL (
                        'Calendar'[Year],
                        'Calendar'[Quarter],
                        'Calendar'[End of quarter date CC]
                    ),
                    "@sales", [Sales this Q:] + 0
                ),
                ORDERBY ( 'Calendar'[End of quarter date CC], ASC )
            )
        )
    )
)

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


View solution in original post

3 REPLIES 3
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

I tried to create a sample pbix file like below.

I hope the below can provide some ideas on how to create a solution for your datamodel.

 

Jihwan_Kim_0-1674360441325.pngJihwan_Kim_1-1674361791511.png

 

Sales Prev Q nonblank: =
IF (
    HASONEVALUE ( 'Calendar'[Quarter] ),
    IF (
        [Sales this Q:] <> BLANK (),
        CALCULATE (
            [Sales this Q:],
            OFFSET (
                -1,
                FILTER (
                    ADDCOLUMNS (
                        ALL (
                            'Calendar'[Year],
                            'Calendar'[Quarter],
                            'Calendar'[End of quarter date CC]
                        ),
                        "@sales", [Sales this Q:] + 0
                    ),
                    [@sales] <> 0
                ),
                ORDERBY ( 'Calendar'[End of quarter date CC], ASC )
            )
        ),
        CALCULATE (
            [Sales this Q:],
            OFFSET (
                -1,
                ADDCOLUMNS (
                    ALL (
                        'Calendar'[Year],
                        'Calendar'[Quarter],
                        'Calendar'[End of quarter date CC]
                    ),
                    "@sales", [Sales this Q:] + 0
                ),
                ORDERBY ( 'Calendar'[End of quarter date CC], ASC )
            )
        )
    )
)

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


Thanks, It was what I needed.

Thanks, It was what I needed.

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.

Top Solution Authors