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.
Hello Internet,
I am working on a problem involving the scope of DAX variables. My code defines the hire date and termination date of employees and them implements logic based on the result. My problem is that the scope of my variable is NOT in the row context of each employee, rather, its shows the minimum hiredate and maximum hiredate for ALL employees. Can someone show me how this could could be modified so that the variables "Hire" and "Term" are set for each individual employee? I suspect an iterator function is missing, but I'm stuck.
thanks!
Solved! Go to Solution.
Hi @BryonScruggs ,
Please follow these steps:
(1) Create a new Table
Employment Cases =
VAR PeriodStart =
DATE ( 2022, 01, 01 )
VAR PeriodEnd =
DATE ( 2022, 03, 31 )
RETURN
ADDCOLUMNS (
SELECTCOLUMNS (
attr_Tkpr_vw,
"EmpID", attr_Tkpr_vw[TkprNumber],
"Hire", attr_Tkpr_vw[TkprDateHire],
"Term", attr_Tkpr_vw[TkprDateTerm],
"Hire Test",
CALCULATE (
MIN ( attr_Tkpr_vw[TkprDateHire] ),
ALLEXCEPT ( attr_Tkpr_vw, 'attr_Tkpr_vw'[TkprNumber] )
),
"Term Test",
CALCULATE (
MIN ( attr_Tkpr_vw[TkprDateTerm] ),
ALLEXCEPT ( attr_Tkpr_vw, 'attr_Tkpr_vw'[TkprNumber] )
)
),
"Employment Period",
SWITCH (
TRUE (),
AND ( AND ( [Hire] >= PeriodStart, [Hire] <= PeriodEnd ), [Term] >= PeriodEnd ), DATEDIFF ( [Hire Test], PeriodEnd, MONTH ),
AND (
AND ( [Hire] >= PeriodStart, [Hire] <= PeriodEnd ),
AND ( [Term] >= PeriodStart, [Term] <= PeriodEnd )
), DATEDIFF ( [Hire Test], [Term Test], MONTH )
)
)
(2)Final output
If that's not what you need, please share the sample file
Best Regards,
Gallen Luo
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi @BryonScruggs ,
Please follow these steps:
(1) Create a new Table
Employment Cases =
VAR PeriodStart =
DATE ( 2022, 01, 01 )
VAR PeriodEnd =
DATE ( 2022, 03, 31 )
RETURN
ADDCOLUMNS (
SELECTCOLUMNS (
attr_Tkpr_vw,
"EmpID", attr_Tkpr_vw[TkprNumber],
"Hire", attr_Tkpr_vw[TkprDateHire],
"Term", attr_Tkpr_vw[TkprDateTerm],
"Hire Test",
CALCULATE (
MIN ( attr_Tkpr_vw[TkprDateHire] ),
ALLEXCEPT ( attr_Tkpr_vw, 'attr_Tkpr_vw'[TkprNumber] )
),
"Term Test",
CALCULATE (
MIN ( attr_Tkpr_vw[TkprDateTerm] ),
ALLEXCEPT ( attr_Tkpr_vw, 'attr_Tkpr_vw'[TkprNumber] )
)
),
"Employment Period",
SWITCH (
TRUE (),
AND ( AND ( [Hire] >= PeriodStart, [Hire] <= PeriodEnd ), [Term] >= PeriodEnd ), DATEDIFF ( [Hire Test], PeriodEnd, MONTH ),
AND (
AND ( [Hire] >= PeriodStart, [Hire] <= PeriodEnd ),
AND ( [Term] >= PeriodStart, [Term] <= PeriodEnd )
), DATEDIFF ( [Hire Test], [Term Test], MONTH )
)
)
(2)Final output
If that's not what you need, please share the sample file
Best Regards,
Gallen Luo
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Try to reference and define the date variables inside an iterator function which take an employee-relevant column as the first argument. for example,
ADDCOLUMNS(
ALL(Employee[EmployeeID]),
...
)
VAR, VAR,...RETURN is just an expression block. A VAR is valid immediately after its definition and till its immdiate next RETURN.
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
41 | |
21 | |
21 | |
14 | |
13 |
User | Count |
---|---|
43 | |
36 | |
33 | |
18 | |
18 |