Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
Hey,
I have this table at Power BI (IdsSemHistorico)
SELECT P.Id
FROM Pessoas P
WHERE NOT EXISTS (
SELECT 1
FROM Pessoas_Historico PH
WHERE PH.IdPessoa = P.Id
AND PH.Ativo IS NOT NULL
)
and then I need a Measure in DAX that give me some calculations. The SQL Query is the following:
SELECT COUNT(*)
FROM Pessoas P
LEFT JOIN IdsSemHistorico I ON P.Id = I.Id
WHERE ISNULL(P.DataAdmissao, P.StampData) <= @MonthStart
AND ISNULL(P.DataRescisao, @MonthEnd) <= @MonthEnd
AND (
(I.Id IS NOT NULL AND P.activo = 'S')
OR (
I.Id IS NULL
AND ISNULL((
SELECT TOP 1 PH.Ativo
FROM Pessoas_Historico PH
WHERE PH.IdPessoa = P.Id
AND PH.DataInicioGrupo <= @MonthStart
AND PH.Ativo IS NOT NULL
ORDER BY PH.DataInicioGrupo DESC
), 1) = 1
)
)
Can anyone help me transform this second query at a DAX expression?
Solved! Go to Solution.
While I don't recommend this approach but here is my attempt :
EmployeeCount =
VAR MonthStart = DATEVALUE("YYYY-MM-DD") -- Replace with your @MonthStart logic
VAR MonthEnd = DATEVALUE("YYYY-MM-DD") -- Replace with your @MonthEnd logic
RETURN
CALCULATE(
COUNTROWS(Pessoas),
FILTER(
Pessoas,
ISBLANK(Pessoas.DataAdmissao) || Pessoas.DataAdmissao <= MonthStart
),
FILTER(
Pessoas,
ISBLANK(Pessoas.DataRescisao) || Pessoas.DataRescisao <= MonthEnd
),
FILTER(
Pessoas,
Pessoas.Activo = "S"
|| (NOT(ISBLANK(Pessoas.Id)) &&
CALCULATE(
COUNTROWS(Pessoas_Historico),
FILTER(
Pessoas_Historico,
Pessoas_Historico.IdPessoa = Pessoas.Id
&& Pessoas_Historico.DataInicioGrupo <= MonthStart
&& NOT(ISBLANK(Pessoas_Historico.Ativo))
),
ALL(Pessoas_Historico),
TOPN(
1,
FILTER(
Pessoas_Historico,
Pessoas_Historico.IdPessoa = Pessoas.Id
&& Pessoas_Historico.DataInicioGrupo <= MonthStart
&& NOT(ISBLANK(Pessoas_Historico.Ativo))
),
Pessoas_Historico.DataInicioGrupo,
DESC
)
) > 0
)
)
)
While I don't recommend this approach but here is my attempt :
EmployeeCount =
VAR MonthStart = DATEVALUE("YYYY-MM-DD") -- Replace with your @MonthStart logic
VAR MonthEnd = DATEVALUE("YYYY-MM-DD") -- Replace with your @MonthEnd logic
RETURN
CALCULATE(
COUNTROWS(Pessoas),
FILTER(
Pessoas,
ISBLANK(Pessoas.DataAdmissao) || Pessoas.DataAdmissao <= MonthStart
),
FILTER(
Pessoas,
ISBLANK(Pessoas.DataRescisao) || Pessoas.DataRescisao <= MonthEnd
),
FILTER(
Pessoas,
Pessoas.Activo = "S"
|| (NOT(ISBLANK(Pessoas.Id)) &&
CALCULATE(
COUNTROWS(Pessoas_Historico),
FILTER(
Pessoas_Historico,
Pessoas_Historico.IdPessoa = Pessoas.Id
&& Pessoas_Historico.DataInicioGrupo <= MonthStart
&& NOT(ISBLANK(Pessoas_Historico.Ativo))
),
ALL(Pessoas_Historico),
TOPN(
1,
FILTER(
Pessoas_Historico,
Pessoas_Historico.IdPessoa = Pessoas.Id
&& Pessoas_Historico.DataInicioGrupo <= MonthStart
&& NOT(ISBLANK(Pessoas_Historico.Ativo))
),
Pessoas_Historico.DataInicioGrupo,
DESC
)
) > 0
)
)
)
Check out the July 2025 Power BI update to learn about new features.
User | Count |
---|---|
72 | |
72 | |
37 | |
31 | |
26 |