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
_lasfera
Frequent Visitor

Getting stuck up in what appears to be a glitch in DAX

I have a table pivoted with 3 columns (and some other calculated ones to test DAXs), and they are 'SENSORDD', 'Attribute' and 'Value', as shown in pic below. 

_lasfera_0-1654849697888.png

And I created a column named 'PreviousValue' to return the values of previous hour for each category ('Attribute') only when 'SENSORDD' fit into 12:XX:XX AM, or else to return 1. The columns worked pretty well except that it returns blank when 'SENSORDD' fitting into a certain condition, and here's how I wrote the code :

 

PreviousValue =
var MinDate =
CALCULATE (
MIN( PRESSURE_NETWORK[SENSORDD] ),
ALLEXCEPT(PRESSURE_NETWORK,PRESSURE_NETWORK[SENSORDD] )) - TIME(1,0,0)
RETURN
IF( HOUR(PRESSURE_NETWORK[SENSORDD]) = 0,
CALCULATE(
SUM ( PRESSURE_NETWORK[Value] ),
FILTER (
ALLEXCEPT ( PRESSURE_NETWORK, PRESSURE_NETWORK[Attribute] ),
PRESSURE_NETWORK[SENSORDD]= MinDate )
),
1
)
_lasfera_3-1654850274612.png_lasfera_4-1654850378888.png_lasfera_5-1654850471790.png

 

As you can see, all the blank values were returned only when 'SENSORDD' was 12:30:00 AM, and I think the tricky part is what the red-colored part in the code, it works completely fine if I erase that part or sub in the parameter TIME(0,0,0), but when I put that back in with some other parameters (e.g. TIME(2,0,0)) ,it starts to not work with some certain 'SENSORDD' value again.

_lasfera_6-1654850854394.png_lasfera_7-1654850923333.png

But Min_date as a calculated column, not as local var, works completely fine.

 

Much thanks in advance.

4 REPLIES 4
tamerj1
Super User
Super User

Hi @_lasfera 
Please try

PreviousValue =
VAR CurrentTime = PRESSURE_NETWORK[SENSORDD]
VAR AttributeTable =
    CALCULATETABLE (
        PRESSURE_NETWORK,
        ALLEXCEPT ( PRESSURE_NETWORK, PRESSURE_NETWORK[Attribute] )
    )
VAR PreviousTable =
    FILTER ( AttributeTable, PRESSURE_NETWORK[SENSORDD] < CurrentTime )
VAR PreviousTime =
    MAXX ( PreviousTable, PRESSURE_NETWORK[SENSORDD] )
VAR PreviousValue =
    MAXX (
        FILTER ( PreviousTable, PRESSURE_NETWORK[SENSORDD] = PreviousTime ),
        PRESSURE_NETWORK[Value]
    )
RETURN
    IF ( HOUR ( CurrentTime ) = 0, PreviousValue, 1 )

Thanks for your reply! But I can have a look at how you would go about it if only the value of an hour ago, rather than 15 minutes ago, is needed. Had many goes at moditication to your code to fit the need but that didn't work out. 

@_lasfera 
You mean like this?

PreviousValue =
VAR CurrentTime = PRESSURE_NETWORK[SENSORDD]
VAR AttributeTable =
    CALCULATETABLE (
        PRESSURE_NETWORK,
        ALLEXCEPT ( PRESSURE_NETWORK, PRESSURE_NETWORK[Attribute] )
    )
VAR PreviousTime =
    CurrentTime - TIME ( 1, 0, 0 )
VAR PreviousValue =
    MAXX (
        FILTER ( AttributeTable, PRESSURE_NETWORK[SENSORDD] = PreviousTime ),
        PRESSURE_NETWORK[Value]
    )
RETURN
    IF ( HOUR ( CurrentTime ) = 0, PreviousValue, 1 )

Yep, but this goes back to where it all started, blank values were returned when 'SENSORDD' was 12:30:00 AM. Appreciate your effort though.🙏

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.