Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

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.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

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.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

Thanks, It was what I needed.

Thanks, It was what I needed.

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.