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
Daveed1973
Advocate II
Advocate II

Dynamic measure based on slicer not calculating totals correctly

I have a measure which is used to dynamically work out the "Filtered Length of Stay" of a Product based on the dates selected in a date slicer. This is the code for the measure...

Filetered Length Of Stay = 
VAR MinDay =
    IF ( ISFILTERED ( 'Calendar'[Date] ), FIRSTDATE ( 'Calendar'[Date] ) )
VAR MaxDay =
    IF ( ISFILTERED ( 'Calendar'[Date] ), LASTDATE ( 'Calendar'[Date] ) )
RETURN
	-- No filter selected, set LengthOfStay to difference between StartDate and TempEndDate
    IF (ISBLANK ( MinDay ), DATEDIFF (MIN( pbi_Usage12Months[StartedAt] ), MAX( pbi_Usage12Months[TempEndedAt] ),DAY) + 1,

		-- If StartDate is after filtered End Date then set length of stay to 0
		IF (MIN ( pbi_Usage12Months[StartedAt] ) > MaxDay, 0, 

			-- If EndDate before the filtered start date then set LOS to 0
			IF ( MIN ( pbi_Usage12Months[TempEndedAt] ) < MinDay, 0, 
				
				-- If the StartDate is after the filtered start date and EndDate is less than the filtered end date -- Workout difference between StartDate and EndDate
				IF (MIN ( pbi_Usage12Months[StartedAt] ) >= MinDay && MIN ( pbi_Usage12Months[TempEndedAt] ) <= MaxDay, DATEDIFF ( MIN ( pbi_Usage12Months[StartedAt] ), MIN ( pbi_Usage12Months[TempEndedAt] ), DAY ) + 1,
                
					-- If StartDate is after filtered start date and EndDate is after filtered end date then workout difference between StartDate and filtered end date
					IF (MIN ( pbi_Usage12Months[StartedAt] ) >= MinDay && MIN ( pbi_Usage12Months[TempEndedAt] ) >= MaxDay, DATEDIFF ( MIN ( pbi_Usage12Months[StartedAt] ), MaxDay, DAY ) + 1,
                        
						-- StartDate is before filtered start date and EndDate before filtered end date workout difference bewteen filtered start date and EndDate
						IF (MIN ( pbi_Usage12Months[StartedAt] ) <= MinDay && MIN ( pbi_Usage12Months[TempEndedAt] ) <= MaxDay, DATEDIFF ( MinDay, MIN ( pbi_Usage12Months[TempEndedAt] ), DAY ) + 1,
                            
							-- StartDate before filtered start date and EndDate after filtered end date workout difference between filtered start and end date
							IF (MIN ( pbi_Usage12Months[StartedAt] ) <= MinDay && MIN ( pbi_Usage12Months[TempEndedAt] ) >= MaxDay, DATEDIFF ( MinDay, MaxDay, DAY ) + 1
                            )
                        )
                    )
                )
            )
        )
    )

 This works correctly. However the issue I have is when I try and display the results graphically or in a table within Power BI Desktop. The total is always either the highest or the lowest number out of the results. I'm assuming this may be because of the VAR MinDay or the VAR MaxDay showing either the FIRSTDATE or the LASTDATE but I may be wrong. I also do not have any summarise functions available as this is a measure.

 

As you can see on this screenshot the Filtered Length of Stay is showing 3 which is the lowest value and not a total sum of the column.

2017-09-06.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Is there a way I can get the Filtered Length Of Stay column to total each row and so that I can perform summarise functions on it?

 

Thanks in advance.

 

 

1 ACCEPTED SOLUTION

@Daveed1973,

 

Add a measure as shown below.

Measure =
IF (
    ISFILTERED ( Usage[ProductType] ),
    [Filtered Length Of Stay],
    SUMX ( Usage, [Filtered Length Of Stay] )
)
Community Support Team _ Sam Zha
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

6 REPLIES 6
v-chuncz-msft
Community Support
Community Support

@Daveed1973,

 

You may try to create a calculated table instead.

Community Support Team _ Sam Zha
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Unfortunately this doesn't seem to work. If I try and create a calculated table based on the same code as above I get an error saying "The expression specified in the query is not a valid table expression". If I simply create a table which is a copy of the Calendar table I only get the Date filed come through and not the "Filtered Length of Stay" measure.

@Daveed1973,

 

Share us a simplified model and your expected result.

Community Support Team _ Sam Zha
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Here is a link to the sample PBIX file.

 

The table and graph that show the Filtered Length of Stay doesn't total correctly. On the screenshot below the total should be 144 and not 0.

 

2017-09-08.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The Full Length of Stay column is simply the number of days between the start date and the end date or the current date + 1. This works our correctly.

@Daveed1973,

 

Add a measure as shown below.

Measure =
IF (
    ISFILTERED ( Usage[ProductType] ),
    [Filtered Length Of Stay],
    SUMX ( Usage, [Filtered Length Of Stay] )
)
Community Support Team _ Sam Zha
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

You Sir are awesome, that has solved the issue. Many thanks for your help!

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.