Forum Discussion
Conditional sum, measures from different tables
Hi
I have two tables with no shared key, but related through properties. The tables themselves would be many-to-many through the properties. As far as I can tell, there is no way for me to join the two tables directly without loosing slicing options.
My problem is that I need to calculate the difference between two sums, one from each of the tables, but with the condition that only the positive differences should count (negative basically set to 0). So the idea is to show a column with sum(quantity in)-sum(quanity out). This is easily done within the table rows, but the total (which is an important measure) wont show the correct value.
Due to the confidentiality of the data, I cannot share too much, however, the data model should be ok to post.
I create a visual containing:
- Kunder[Kunde]
- Mottaksstasjon[Mottaksstasjon]
- Mottaksstasjon[Sted]
- Kalender[Uke]
With measures:
- Sum(Salg[Kvantum])
- Sum(Seddel[Kvantum])
- Sum(If(Sum(Salg[Kvantum])-Sum(Seddel[Kvantum])<0,0,Sum(Salg[Kvantum])-Sum(Seddel[Kvantum])))
Measure 3 is the problematic one, I cannot get the total correct, please help.
Kind regards,
Ståle
4 Replies
- amitchandakSuper User
Ståle , fro correct total you have force a correct context like
sumx(kanendar, Sum(If(Sum(Salg[Kvantum])-Sum(Seddel[Kvantum])<0,0,Sum(Salg[Kvantum])-Sum(Seddel[Kvantum]))))
or , based on the group/axis in the visual, example
sumx(values(kanendar[date]), Sum(If(Sum(Salg[Kvantum])-Sum(Seddel[Kvantum])<0,0,Sum(Salg[Kvantum])-Sum(Seddel[Kvantum]))))
- StåleNew Member
Thx for your quick reply amitchandak !
I dont think this solution would work. I dont have a "table" to use sumx on. If I try, also the individual rows calculates incorrectly, I believe due to the many-to-many issue. It also returns wrong results when excluding the conditional and trying a simpler form for testing:
SUMX(VALUES(Kalender[date]),SUM(Salg[Kvantum])-SUM(Seddel[Kvantum]))
In my head, the logic must be something like:
- Calculate the aggregates on row to row basis from the visual, and "not" from any table.
- Calculate the difference and check if it is positive
- Sum up all positive differences as if the visual itself was a table
I tried to use summarize to turn visual into a temp table, but couldnt make it work either.
Best regards,
Ståle
- amitchandakSuper User
Ståle , In power bi grand total is re calculated. if there is any row condition that used for calculation will not give correct grand total
refer: https://www.youtube.com/watch?v=ufHOOLdi_jk
refer to how common dimension has been used for correct avg.