Forum Discussion
SUMX with Filter or If Statement
- 6 years ago
Hi, @AMR80
Sorry for my late response.
It's not recommended that you calculate the aggregate value in Power Query. Power Query uses M language to write formulas,and using custom column will change the original data model.
You can create a measure in the model. The measure is mainly used for aggregation calculation, and the calculations are executed during query.
Like this:
Measure = SUMX ( FILTER ( 'Table_query__5', AND ( [Date] > TODAY () - 365 && [Date] <= TODAY (), [Type of Time] = "Family Leave" ) ), [Hours Requested] )If you still have questions, please feel free to contact me.
Best Regards
Janey Guo
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
This is great v-janeyg-msft. The measure worked perfectly to calculate the FMLA time used 🙂 Now my challenge is to take this measure to the next level. What would be really great is if I could get a Measure that calculated this:
If type of time is "family leave", do this:
If type of time is "personal hours", do this:
For all other types of time, do this: Sum of all hours used in the current year.
Hi, AMR80
According to your description, I think you can use the switch function to combine these three formulas in one measure.
Like this:
Measure =
SWITCH (
SELECTEDVALUE ( Table_query__5[Type of Time] ),
"Family Leave",
SUMX (
FILTER ( Table_query__5, [Date] > TODAY () - 365 && [Date] <= TODAY () ),
[Hours Requested]
),
"personal hours",
SUMX (
FILTER (
Table_query__5,
[Date] > [Personal Hours Reset Date] - 365
&& [Date] <= TODAY ()
),
[Hours Requested]
),
SUMX (
FILTER ( Table_query__5, YEAR ( [Date] ) = YEAR ( TODAY () ) ),
[Hours Requested]
)
)
Due to the complexity of the measure, total cann't get the correct result in the context, you can refer to this article:
https://community.powerbi.com/t5/DAX-Commands-and-Tips/Dealing-with-Measure-Totals/td-p/63376
IF you have other questions,please feel free to contact me.
Best Regards
Janey Guo
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.