Why does my measure with SumX return this result? - powerbi

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
)

Related

DAX Calcuate rolling sum

I have a problem with calculating measure that sums values for 3 previous periods.
Below I attach sample fact table and dict table to show problem I am facing.
date
customer
segment
value
01.01.2021
1
A
10
02.01.2021
1
A
10
03.01.2021
1
A
10
04.01.2021
1
A
10
01.01.2021
2
B
20
02.01.2021
2
B
30
03.01.2021
2
B
40
dict table:
segment
segment_desc
A
Name of A
B
Name of B
Approach I have taken:
last 3 value =
VAR DATES = DATESINPERIOD(facts[date],LASTDATE(facts[date]), -3,MONTH)
RETURN CALCULATE([sum value], DATES)
It produces correct results as long as there is at least one record for April.
When I use filter on segment_desc = 'B'
It produces result as I attached - so we see result in April equals 20, which is obviously not what I wanted. I would expect it to be 50.
Answer to the main question:
time intelligence functions like DATESINPERIOD require a proper calendar table, because they expect continuous dates, without gaps.
Answer to the follow-up question "why the measure shows value for January?"
It's a bit tricky. First, notice that LASTDATE in this filter context returns blank:
So, your DAX measure then becomes this:
last 3 value =
VAR DATES = DATESINPERIOD(facts[date], BLANK(), -3,MONTH)
RETURN CALCULATE([sum value], DATES)
Blank - 3 month does not make sense, so the way DAX resolves this: it replaces BLANK with the first (min) date in the table. In this case, it's 1/1/2021. Then it goes back 3 months from that date. As a result, the final measure is:
last 3 value =
CALCULATE([sum value], {2020-11-01, 2020-12-01, 2021-01-01 })
Since you have no data prior to 2021-01-01, the final result shows only January values.

DAX Summing Values in Another Table

I am new to DAX.
I have 2 tables. Let's call them Table_1 and Table_2.
Let's say they look like this:
Table_1
ID Table_2_ID Person
1 1 Steve
2 1 Steve
3 1 Steve
4 2 John
5 2 John
6 3 Sally
Table_2 Sales
1 100
2 50
3 5
I want to return results that look something like this:
ID Table_2_ID Person Sales
1 1 Steve 100
2 1 Steve 100
3 1 Steve 100
4 2 John 50
5 2 John 50
6 3 Sally 5
How can I return this with a Dax function?
I know I need to use LOOKUPVALUE and/or the RELATED function, in combination with SUM, but I'm not sure how.
I'm not looking to produce a table, but a measure that when I use it in combination with other columns in Power BI, it applies the appropriate amount to each person in Table_1.
This can be done either by a calculated column or by a measure.
CC in Table_1:
Sum_Tab2 =
var t2_ID = [Table_2_ID]
return
CALCULATE(
SUM('Tabel_2'[Sales]),
'Tabel_2'[ID] = t2_ID
)
Measure:
SumTab2_measure =
var currentT2ID = MAX('Tabel_1'[Table_2_ID])
return
CALCULATE(
SUM('Tabel_2'[Sales]),
'Tabel_2'[ID] = currentT2ID
)
No relationships needed. However, for the measure to work in a visual table the [Tabel_2_ID from Tabl_1 needs to be present with this solution.
These may have to be slightly altered depending on your other filter dependencies and such so that they behave as you want them to.

Make first few rows unresponsive to slicer Power BI

I have a data which looks like below:
Brands Sales Category Index
Brand1 588 A 1
Brand2 846 A 2
Brand3 827 A 3
Brand4 951 A 4
Brand5 673 B 5
Brand6 637 B 6
Brand7 575 B 7
Brand8 995 B 8
Btand9 737 C 9
Brand10 661 C 10
Brand11 729 C 11
Brand12 789 C 12
Brand13 836 C 13
Problem statement :
I am trying to put Category as a slicer. However I want the rows for Category A to be present in the table view irrespective of the slicer which is selected.
Example: Lets say if Category B is selected in slicer , in this case the table should return all rows until Rank 8.
Below is an example of the desired output when category C is selected:
As you can see, the visual table has both Category A and Category C.
Similarly when both B and C are selected, I should be able to display all the categories (A,B and C).
What tried:
I was thinking if we can use a conditional DAX which return 1 for selected values in slicers and mark rest as 0, I could use that as a visual filter and filter out 0. I tried various combinations of Filter with in Filters and SELECTCOLUMN but it did not work. Even the below measure returns all the rows instead of Selected values|| category="A"
test1 = CALCULATE(MIN('Table'[Index]),FILTER(ALLEXCEPT('Table','Table'[Brands]),'Table'[Category]=SELECTEDVALUE('Table'[Category]) || 'Table'[Category]="A"))
I also tried something like:
test = var cat = min('Table'[Category]) return IF(cat = SELECTEDVALUE('Table'[Category])||cat="A",1,0)
But this gives all as 1 , doesnot give 0 for rows which does not match the condition (note I have blocked the slicer interaction here)
Any help would be highly appreciated.
First, you need to separate your slicer table as keeping the value in the same table you can not achieve the requirement. You can create custom table with this below code-
considering your base table name sales
Lets create the custom table category list
No relation can be there between table sales and category list
category list =
SELECTCOLUMNS(
sales,
"Category",sales[Category]
)
Now, create the slicer using new custom table category list and create this below Measure-
is filter =
if(
MIN(sales[Category]) = "A",
1,
if (
MIN(sales[Category]) IN VALUES('category list'[Category]),
1,
0
)
)
Here below is a sample output when C selected-

DAX selecting and displaying the max value of all selected records

Problem
I'm trying to calculate and display the maximum value of all selected rows alongside their actual values in a table in Power BI. When I try to do this with the measure MaxSelectedSales = MAXX(ALLSELECTED(FactSales), FactSales[Value]), the maximum value ends up being repeated, like this:
If I add additional dimensions to the output, even more rows appear.
What I want to see is just the selected rows in the fact table, without the blank values. (i.e., only four rows would be displayed for SaleId 1 through 4).
Does anyone know how I can achieve my goal with the data model shown below?
Details
I've configured the following model.
The DimMarket and DimSubMarket tables have two rows each, you can see their names above. The FactSales table looks like this:
SaleId
MarketId
SubMarketId
Value
IsCurrent
1
1
1
100
true
2
2
1
50
true
3
1
2
60
true
4
2
2
140
true
5
1
1
30
false
6
2
2
20
false
7
1
1
90
false
8
2
2
200
false
In the table output, I've filtered FactSales to only include rows where IsCurrent = true by setting a visual level filter.
Your max value (the measure) is a scalar value (a single value only). If you put a scalar value in a table with the other records, the value just get repeated. In general mixing scalar values and records (tables) does not really bring any benefit.
Measures like yours can be better displayed in a KPI or Multi KPI visual (normally with the year, that you get the max value per year).
If you just want to display the max value of selected rows (for example a filter in your table), use this measure:
Max Value = MAX(FactSales[Value])
This way all filter which are applied are considered in the measures calculation.
Here is a sample:
I've found a solution to my problem, but I'm slightly concerned with query performance. Although, on my current dataset, things seem to perform fairly well.
MaxSelectedSales =
MAXX(
FILTER(
SELECTCOLUMNS(
ALLSELECTED(FactSales),
"id", FactSales[SaleId],
"max", MAXX(ALLSELECTED(FactSales), FactSales[Value])
),
[id] = MAX(FactSales[SaleId])
),
[max]
)
If I understand this correctly, for every row in the output, this measure will calculate the maximum value across all selected FactSales rows, set it to a column named max and then filter the table so that only the current FactSales[SaleId] is selected. The performance hit comes from the fact that MAX needs to be executed for every row in the output and a full table scan would be done when that occurs.
Posted on behalf of the question asker

Power BI - How to find a maximum value from a list of sum

In PowerPivot function or Power BI, for data set
article channel qty
1 a 5
1 b 8
1 c 10
2 a 6
2 b 9
2 c 12
I want to create a measure "Maximum net" stands for the maximum net qty of 2 articles in all channels(including "a", "b" & "c").
How to make the measure first sum up all the qty of 1 & 2 articles then find the maximum value of the 2 sums?
I tried to use the following DAX code
=MAXX(VALUES("table[article]"),SUM([qty]))
but the final output is 50. What I suppose the formula do should be the first sum on 2 articles get the "5+8+10=23" & "6+9+12=27", then find the maximum of "23" & "27" and finally get "27"
The DAX below first groups on article and then takes the max:
Measure = MAXX(GROUPBY('table';'table'[article];"total";SUMX(CURRENTGROUP();'table'[qty]));[total])
You can also go with a seperate table and use this:
ArticleTable = GROUPBY('table';'table'[article];"total";SUMX(CURRENTGROUP();'table'[qty]))