cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
andersrys
Frequent Visitor

Using DAX to make a column: how to filter using a value in the current row

I have a table including the column "time", "sensor_id" and "temperature". Every row in the table consist of a unique combination of time and sensor_id, but both columns have repeating values. I want to make a new column showing the time difference between the row's time and the time of the sensor's maximum temperature. However, I do not manage to do this seperately for every sensor. Instead, I only use the time of the maximum temperature overall- neglecting sensor_id. The code: 
-----------------------------------------------------------

CloseToPeakTemp =
Var timeofMaxTemperature=CALCULATE(FIRSTNONBLANK(NeuronData[time_observed],true),
Filter(NeuronData,NeuronData[sensor_id]=SELECTEDVALUE(NeuronData[sensor_id])),
FILTER(NeuronData,MAX(NeuronData[WireTemperature])=NeuronData[WireTemperature]))

Var timeFromMaxTemp=ABS(NeuronData[time_observed]-timeofmaxTemperature)
return if(timeFromMaxTemp<TIMEVALUE("00:05"),TRUE,FALSE)
-----------------------------------------------------------
I know that the max function here, in the second Filter function, calculates the maximum temperature of all the measurements. I therefore want to change this to the maximum value for the row's sensor. SELECTED value is just an attempt to filter for the current row's sensor, but this won't be helpful without the sensor's maximum temperature as well. As the code is now, only the sensor with the maximum temperature will have correct values (assuming SelectedValue actually works). The other sensor's will just display the time differance between their row's time and the time of the other sensor's maximum temperature, which is useless information. To solve the problem using the current code, I would need to filter for the current row's sensor_id before finding the maximum- is that possible?

Alternatively, does anyone have another way of solving this problem? I have tried the GroupBy function to find the max temperatures, but I never managed to utilize the resulting table variable. 

1 ACCEPTED SOLUTION
lbendlin
Super User III
Super User III

Something like this

CloseToPeakTemp =
Var SID=SELECTEDVALUE(NeuronData[sensor_id])
Var timeofMaxTemperature=CALCULATE(FIRSTNONBLANK(NeuronData[time_observed],true),
Filter(NeuronData,NeuronData[sensor_id]=SID),
FILTER(NeuronData,MAX(NeuronData[WireTemperature])=NeuronData[WireTemperature]))
Var timeFromMaxTemp=ABS(NeuronData[time_observed]-timeofmaxTemperature)
return if(timeFromMaxTemp<TIMEVALUE("00:05"),TRUE,FALSE)

View solution in original post

3 REPLIES 3
lbendlin
Super User III
Super User III

Something like this

CloseToPeakTemp =
Var SID=SELECTEDVALUE(NeuronData[sensor_id])
Var timeofMaxTemperature=CALCULATE(FIRSTNONBLANK(NeuronData[time_observed],true),
Filter(NeuronData,NeuronData[sensor_id]=SID),
FILTER(NeuronData,MAX(NeuronData[WireTemperature])=NeuronData[WireTemperature]))
Var timeFromMaxTemp=ABS(NeuronData[time_observed]-timeofmaxTemperature)
return if(timeFromMaxTemp<TIMEVALUE("00:05"),TRUE,FALSE)

View solution in original post

lbendlin
Super User III
Super User III

you should not use SELECTEDVALUE() inside CALCULATE(). Use variables instead.

Ok, so how can I find the sensor_id for the row and use it as a variable? That would solve all my problems!

Helpful resources

Announcements
PBI User Groups

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

April Update

Check it Out!

Click here to read more about the April 2021 Updates!

MBAS Carousel

Sign up for our May 4th event!

May the fourth be with you, join us online!

secondImage

The largest Power BI virtual conference

100+ sessions, 100+ speakers, Product managers, MVPs, and experts. All about Power BI. Attend online or watch the recordings.

Top Solution Authors