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
jshankle89
New Member

Comparing Date from Var

Hello,

I'm trying to return the current battery percentage for each LeakDetector.  A device checks in multiple times per day with its battery remaining percentage and is logged in a Checkins table.  I'm using direct query on the source data.  I tried the below code and its returning blank results.  If I replace lastCheckin in the filter with a hard coded dateTime value that I know is viable, then it returns the value I want. I dont understand why the var cant be used to determine the date.  Does anyone see an apparant issue?

 

*Note: I used max to determine var detector because I needed a single value, and I'm viewing this measure in a table next to the LeakDetectorId's, so that leaves me with a single value to choose from.  This seems to work fine.

 

CurrentBatteryLevel = 
var lastCheckin = max(Checkins[OccurredAt])
var detector = max(Checkins[LeakDetectorId])
return 
CALCULATE(Min(Checkins[BatteryLevel]),filter(Checkins, Checkins[LeakDetectorId] = detector && Checkins[OccurredAt] = lastCheckin))
1 ACCEPTED SOLUTION
v-shex-msft
Community Support
Community Support

HI @jshankle89,

 

You can try to use below formula if it works on your side.

CurrentBatteryLevel =
VAR lastCheckin =
    MAX ( Checkins[OccurredAt] )
VAR detector =
    MAX ( Checkins[LeakDetectorId] )
RETURN
    MINX (
        FILTER (
            ALL ( Checkins ),
            [LeakDetectorId] = detector
                && [OccurredAt] = lastCheckin
        ),
        [BatteryLevel]
    )

If above not help, please share some sample data for further test.

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

2 REPLIES 2
v-shex-msft
Community Support
Community Support

HI @jshankle89,

 

You can try to use below formula if it works on your side.

CurrentBatteryLevel =
VAR lastCheckin =
    MAX ( Checkins[OccurredAt] )
VAR detector =
    MAX ( Checkins[LeakDetectorId] )
RETURN
    MINX (
        FILTER (
            ALL ( Checkins ),
            [LeakDetectorId] = detector
                && [OccurredAt] = lastCheckin
        ),
        [BatteryLevel]
    )

If above not help, please share some sample data for further test.

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

I'm going to do some deeper testing, but this seems to do exactly what I needed.  Thanks so much!  I will look into the documentation to learn the difference between the Min and MinX functions, in addition to the use of the All function.  This was one of the first Power BI reports I have created, so its been a great learning experience.

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.