Forum Discussion
DAX sum by distinct values
There are two tables from two data sources:
Table1(Field1, Field2, Field3, Field4, Field5, Field7)
Table2(Field1, Field2, Field3, Field7)
Next I collect agregate table "agregate_table" as:
select t1.Field1, t1.Field2, t1.Field3, t1.Field4, t1.Field5, t2.cnt as cnt_all, t1.cnt
from (select Field1, Field2, Field3, Field4, Field5, count(distinct Field7) as cnt
from Table1
group by Field1, Field2, Field3, Field4, Field5) as t1
left join (select Field1, Field2, Field3, count(distinct Field7) as cnt
from Table2
group by Field1, Field2, Field3) as t2 on t2.Field1 = t1.Field1 and t2.Field2 = t1.Field2 and t2.Field3 = t1.Field3
, t1.Field2
, t1.Field3
, t1.Field4
, t1.Field5
, t1.cnt / t2.cnt_all as ratio
from (
select Field1, Field2, Field3, Field4, Field5, sum(cnt) as cnt
from agregate_table
group Field1, Field2, Field3, Field4, Field5
) as t1
left join (
select Field1, Field2, Field3, sum(cnt_all) as cnt_all
from (
select distinct Field1, Field2, Field3, cnt_all
from agregate_table
) as t
group by Field1, Field2, Field3
) as t2 on t2.Field1 = t1.Field1 and t2.Field2 = t1.Field2 and t2.Field3 = t1.Field3
how to implement it in DAX?
2 Replies
- lbendlin
Super User
Provide sample data and expected outcome.
Note that both ALL() and VALUES() give you distinct collections. As does SUMMARIZE()
- v-lionel-msft
Community Support
Hi wvadik ,
You can use 'Merge Queries' to combine the two tables first.
Best regards,
Lionel ChenIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly.