I would like to create measure which will calculate sum of sales orders.
I have a table:
Sales Order ID
Sales Order Item
Type
Amount
1
01
1
250
1
02
2
300
2
01
1
100
3
01
2
50
If one Sales Order has 1 and 2 type then should be calculated only type 2, when Sales Order has only one type then all value should be calculated. So in that case:
Sales Order 1 = 300
Sales Order 2 = 100
Sales Order 3 = 50
Could you please help me how can I achieve this results?
Best regards
Probably this is what you need:
SumConditional =
var __typ2 = ADDCOLUMNS( SUMMARIZE('Sheet1 (2)', 'Sheet1 (2)'[Sales Order ID]), "MaxType", CALCULATE(MAX('Sheet1 (2)'[Type]) ))
return
CALCULATE( SUM('Sheet1 (2)'[Amount]), TREATAS( __typ2,'Sheet1 (2)'[Sales Order ID],'Sheet1 (2)'[Type]) )
Related
I have 3 measures:
1) total_trx = SUM(mytable[trx])
2) trx_prev_month = CALCULATE([total_trx], DATEADD(calendar[date], -1,MONTH))
3) monthly_var = DIVIDE([total_trx],[trx_prev_month])-1
If I add a waterfall viz, x-axis with month, it gives me the % of monthly variation and a TOTAL bar at the end that sums all the variations.
I need to reproduce that total number in order to show a KPI as in "so far, we've increased ...%", changing when using a date slicer.
Seems like sum(monthly_var) is not allowed.
Any ideas?
Thank you very much.
Edit1: sample with date filter = Last 4 months
Jul 100 0%
Aug 110 10%
Sep 90 -20%
Oct 80 -10%
Total: -20% <- need a dax to calculate this number and show just -20%
Then if I change the filter to, for example LAST 6 MONTHS, I need to calculate it up to May
In order to get the desired result we will use an intermediate table in our query that will summarize the results by months:
use this code and replace calendar[Year Month] with your Year month column :
SUMX(
SUMMARIZECOLUMNS(calendar[Year Month],"Monthly_int_var",[monthly_var]),
[Monthly_int_var]
)
I have 2 tables:
**Partners**
PartnerID Name
1 AAAA
2 BBBB
3 CCCC
4 DDDD
**Sales**
PartnerID SaleAmount
1 15
2 20
3 30
4 40
1 15
I have a visual with PartnerID from my Partners table and this measure:
TotalSalesMeasure: Sumx(Partners, Sum(Sales[SaleAmount]))
**Resulting table visual with measure**
PartnerID TotalSalesMeasure
1 30
2 20
3 30
4 40
What's confusing me is how the results are derived. It's my understanding that:
-The partners table is filtered using the incoming context(PartnerID)
-Filtered partners table is iterated through and Sum(Sales[SaleAmount]) is called for each row
-After Iteration is done, it is summed
First row ex:
-Partners is filtered to 1 row based on the partnerID
-Since it's using row context and not filter context in the partners table, it sums the entire Sales[SaleAmount] column one time
-That should result in 15+20+30+40+15 = 120, but it shows 30
I was basing this on a video here at around the 49 min mark where he does a similar operation:
https://www.youtube.com/watch?v=1yWLhxYoq88
What's odd is if I do the same thing he does later by wrapping Sum with Calculate, I get the same result(aside from the totals field). In fact, my result seems to be what calculate would return(again, outside of the grand total)
I'm obviously confusing something, but I don't know what it is
Edit:
I think I know what's going on now. The external filter context is applied to the sales table before summing. I didn't realize that it did that as well
SumX(
Partners <---Affected by exterior context
Sum(Sales[SalesAmount]) <---Affected by exterior context + row context
)
I am trying to create a measure that calculates (a/qty)*100 for each month,
where qty commes from Delivery table (generated with an R script)
month qty
<date> <dbl>
1 2019-02-01 1
2 2019-03-01 162
3 2019-04-01 2142
4 2019-05-01 719
And a comes from a table TABLE_A that has been created within Power BI that looks like this :
Client Date a
x 2019-03-07 3
x 2019-04-14 7
y 2019-03-12 2
So far, I managed to calculate that value overall with the following measure formula :
MEASURE = CALCULATE( (Sum(TABLE_A[a])/sum(Delivery[qty]))*100)
The issue I have is that I would need this measure monthly (i.e. join the tables on month) without explicitly defining a link between the tables in the PowerBI model.
For each row in TABLE_A you need to look up the corresponding qty in Delivery, so try something along these lines:
MEASURE =
DIVIDE(
SUM( TABLE_A[a] ),
SUMX(
TABLE_A,
LOOKUPVALUE(
Delivery[qty],
Delivery[month], EOMONTH( TABLE_A[Date], -1 ) + 1
)
)
) * 100
The formula EOMONTH( TABLE_A[Date], -1 ) returns the end of the previous month relative to that date and adding 1 day to that to get the start of the current month for that date.
Problem
I would like to create a multi-layer histogram that shows the distribution of var1 on the first level and var2 on the second level, with a legend by source, like this:
The value should show the percentage w.r.t. the total of a source, with all the selections and slicers applied. The percentages shown in the histogram should always sum to 100% per source.
Example data
I have the following example data:
source var1 var2 count
A 1 1 100
A 1 2 12
A 1 3 34
A 2 1 1612
A 2 2 23
A 2 3 43
B 1 1 200
B 1 2 320
B 1 3 12
B 2 1 1757
B 2 2 345
B 2 3 32
What have I tried
I can achieve a total per source with the following measure without the filtering part:
percPerSource =
DIVIDE(
SUM(input[count]);
CALCULATE(
SUM(input[count]);
ALLEXCEPT(input;input[source])
)
)*100
If I turn on Drill mode and click on the columns of var1 I get the following, undesired result (the percentages do not sum to 100%):
Another attempt was using the ALLSELECTED function:
percSelected =
DIVIDE(
SUM(input[count]);
CALCULATE(
SUM(input[count]);
ALLSELECTED(input[var1])
)
)*100
This shows only 100% on the var2 level:
I think this will do what you're after:
percPerSource =
DIVIDE(
SUM(input[count]);
CALCULATE(
SUM(input[count]);
FILTER(
ALLSELECTED(input);
input[Source] IN VALUES(input[Source])
)
)
)*100
This takes all the selected values as the universe you are filtering on but only selects the rows that in your local filter context.
The FILTER function takes a table as its first argument and a condition as the second argument. It iterates through every row in the table passed into it and checks if the condition holds and returns a table with only the rows where the condition evaluates to True.
The VALUES function returns a list of distinct values of the column specified evaluated within the local filter context.
SalePersonId Month Qty
1 Jan-18 5
2 Jan-18 7
1 Feb-18 1
2 Feb-18 8
3 Feb-18 12
I need to create a measure which gives me a count of salespersons whose total sales quantity is more than 10 for the year 2018.
The result should be 2 (Sale person 1 & 3)
I can achieve this in T-SQL with the following query:
SELECT COUNT(Distinct EmpId) FROM T1 GROUP BY UserId HAVING SUM(Qty) > 10
How can I do the same in DAX?
Here's one possible approach:
= COUNTROWS(
FILTER(
SUMMARIZECOLUMNS(
T1[SalePersonId],
"Total", SUM(T1[Qty])),
[Total] > 10))
The SUMMARIZECOLUMNS part is essentially
SELECT SalePersonId, SUM(Qty) AS 'Total' FROM T1 GROUP BY SalePersonId
The FILTER part is equivalent to the HAVING clause and then you just count the rows in the resulting table.