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
Anonymous
Not applicable

Measure: Calculate Category conditioned and Filtered Values

Hello,

 

I am struggling getting a measure to work.

Could you please help me.

 

I would like to create the following measure:

 

Find the largest Timestamp. => 27.07.2019 00:17

Get the corresponding category. => Z

Sum up all values for this category Z. => 3

Find the Minimum Timestamp for category Z. => 26.07.2019 20:17

 

Calculate time diffrence. => 4h

Divide the sumed values by the time difference. => 3/4

 

 

 

TimestampCategoryValue
26.07.2019 13:17X0
26.07.2019 14:17X1
26.07.2019 15:17X1
26.07.2019 16:17X1
26.07.2019 17:17Y0
26.07.2019 18:17Y0
26.07.2019 19:17Y0
26.07.2019 20:17Z0
26.07.2019 21:17Z1
26.07.2019 22:17Z1
26.07.2019 23:17Z1
27.07.2019 00:17Z0

 

 

Could anyone help me with this. I searched a lot for DAX Functions CALCULATE and FILTER but i could not find a solution.

 

Than kyou very much in advance.

 

Best regards

 

Sebastian

1 REPLY 1
Anonymous
Not applicable

 -- If your table is big, then you should not store
-- timestamps in one column. You should have separate
-- columns for the date and for the time. The reason
-- being that such a column would be dense (big number
-- of unique values) and as such it would not be well
-- compressed, hence calculations involving it would
-- be slow out of necessity.


[Measure] =
var __maxTimestamp = MAX( T[Timestamp] )
var __maxTimestampCategory =
-- this should return a single value
-- if not, then you might have data quality
-- problems or your algorithm does not take
-- into account all possibilities
    CALCULATE(
        VALUES( T[Category] ),
        T[Timestamp] = __largestTimestamp
    )
var __minTimestampForCategory =
    CALCULATE(
        MIN ( T[Timestamp] ),
        T[Category] = __maxTimestampCategory
    )
var __timeDiffInHours =
    DATEDIFF (
        __minTimestampForCategory,
        __maxTimestamp,
        HOUR
    )
var __sum =
    CALCULATE(
        SUM ( T[Value] ),
        T[Category] = __largestTimestampCategory  
    )
var __result =
    DIVIDE( __sum, __timeDiffInHours )
return
    __result

Try this measure. It might or might not do what you want depending on your intentions which are not fully clear at this moment. For instance, what should happen when you slice the data by some attributes?

 

Best

Darek

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