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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch 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
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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.