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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Anonymous
Not applicable

Coalesce excluding pre and post data

Hi, 
This formula works perfect if is inscope, meaning if I select the start date and last date of the analysed item;
Without Date filters the formula calculates ALL zero's from the 1st day of my calendar date till last: December 2022. With this my averages are way off. How can this formula be improved please? Thank you 

Average COALESCE =
IF (
    NOT ISEMPTY ( 'FACT TABLE' ),
    AVERAGEX (
        VALUES ( 'Calendar'[Date] ),
        COALESCE ( [SDR ID average per CP ID], 0 )
    )
)
 
 
1 ACCEPTED SOLUTION

the DATESBETWEEN was in the wrong place

Average Connector Usage =
IF (
    NOT ISEMPTY ( 'FACT TABLE' ),
    VAR minDate =
        MIN ( 'FACT TABLE'[Start Date] )
    VAR maxDate =
        MAX ( 'FACT TABLE'[Start Date] )
    RETURN
        CALCULATE (
            AVERAGEX (
                VALUES ( 'Calendar'[Date] ),
                COALESCE ( [SDR ID average per CP ID], 0 )
            ),
            DATESBETWEEN ( 'Calendar'[Date], minDate, maxDate )
        )
)

View solution in original post

8 REPLIES 8
johnt75
Super User
Super User

You could try

Average COALESCE =
IF (
    NOT ISEMPTY ( 'FACT TABLE' ),
    VAR minDate =
        MIN ( 'FACT TABLE'[Date] )
    VAR maxDate =
        MAX ( 'FACT TABLE'[Date] )
    RETURN
        CALCULATE (
            AVERAGEX (
                VALUES ( 'Calendar'[Date] ),
                COALESCE ( [SDR ID average per CP ID], 0 )
            )
        ),
    DATESBETWEEN ( 'Date'[Date], minDate, maxDate )
)
Anonymous
Not applicable

Hi johnt75,
Thank you so much. I've tried your formula, editing the FACT TABLE 

Average Connector Usage =
IF (
    NOT ISEMPTY ( 'FACT TABLE' ),
    VAR minDate =
        MIN ( 'FACT TABLE'[Start Date] )
    VAR maxDate =
        MAX ( 'FACT TABLE'[Start Date] )
    RETURN
        CALCULATE (
            AVERAGEX (
                VALUES ( 'Calendar'[Date] ),
                COALESCE ( [SDR ID average per CP ID], 0 )
            )
        ),
    DATESBETWEEN ( 'Calendar'[Date], MIN('Calendar'[Date], MAX('Calendar'[Date])
)

I receive a warning : too few arguments for the DATESBETWEEN function. 
I guess I got lost on DATESBETWEEN because I'm not sure what these are: 
( 'Date'[Date], minDate, maxDate )

I've only got available [Date] options: 

'FACT TABLE'[Date]

or

'Calendar'[Date]

minDate and maxDate are the variables created earlier in the code. You want to find the earliest and latest dates from your fact table, not your calendar table - your calendar table will likely have dates before and after you have any facts

Average Connector Usage =
IF (
    NOT ISEMPTY ( 'FACT TABLE' ),
    VAR minDate =
        MIN ( 'FACT TABLE'[Start Date] )
    VAR maxDate =
        MAX ( 'FACT TABLE'[Start Date] )
    RETURN
        CALCULATE (
            AVERAGEX (
                VALUES ( 'Calendar'[Date] ),
                COALESCE ( [SDR ID average per CP ID], 0 )
            )
        ),
    DATESBETWEEN ( 'Calendar'[Date], minDate, maxDate )
)
Anonymous
Not applicable

Thank you johnt75, 
That makes sense. 
I've edited my formula, but I am receiving an error: The syntax for ',' is incorrect;
Please check my formula and see what I have typed incorrectly? Thank you so much.

Average Connector Usage =
IF (
    NOT ISEMPTY ( 'FACT TABLE' ),
    VAR minDate =
        MIN ( 'FACT TABLE'[Start Date] )
    VAR maxDate =
        MAX ( 'FACT TABLE'[Start Date] )
    RETURN
        CALCULATE (
            AVERAGEX (
                VALUES ( 'Calendar'[Date] ),
                COALESCE ( [SDR ID average per CP ID], 0 )
            )
        ),
    DATESBETWEEN ('Calendar'[Date], VAR minDate, VAR maxDate )
)

You only put the VAR keyword the first time you are declaring the variable, you don't use it when using the variable later in the formula

Average Connector Usage =
IF (
    NOT ISEMPTY ( 'FACT TABLE' ),
    VAR minDate =
        MIN ( 'FACT TABLE'[Start Date] )
    VAR maxDate =
        MAX ( 'FACT TABLE'[Start Date] )
    RETURN
        CALCULATE (
            AVERAGEX (
                VALUES ( 'Calendar'[Date] ),
                COALESCE ( [SDR ID average per CP ID], 0 )
            )
        ),
    DATESBETWEEN ( 'Calendar'[Date], minDate, maxDate )
)
Anonymous
Not applicable

I've copied your formula and I get error message: 
'Failed to resolve name 'minDate'. It is not valid table, variable or function name. 

the DATESBETWEEN was in the wrong place

Average Connector Usage =
IF (
    NOT ISEMPTY ( 'FACT TABLE' ),
    VAR minDate =
        MIN ( 'FACT TABLE'[Start Date] )
    VAR maxDate =
        MAX ( 'FACT TABLE'[Start Date] )
    RETURN
        CALCULATE (
            AVERAGEX (
                VALUES ( 'Calendar'[Date] ),
                COALESCE ( [SDR ID average per CP ID], 0 )
            ),
            DATESBETWEEN ( 'Calendar'[Date], minDate, maxDate )
        )
)
Anonymous
Not applicable

Oh my gosh johnt75; It actually works!!!! 😄
I am ever so grateful. Thank you so so so very much!!!!!!! ❤️ 😉 

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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

Top Kudoed Authors