How to remove duplicates in DAX but select the one not blank in DAX? - powerbi

I have a line of code that looks like this
DISTINCT(
UNION(
SUMMARIZE(
'Table1','Table1'[Id] ,"Value",DISTINCTCOUNT('Table1'[Value])),
DISTINCT(SELECTCOLUMNS('Table2',"ID",'Table2'[ID],"Number of Value",BLANK()))
))
In the results I have stuff values that look like this
ID | Value
1 16
2 22
3 25
3
4 16
5 49
5
Is there a way to end up with a table like this? I would use power query but these are created tables from DAX.

Related

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.

Why does my measure with SumX return this result?

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
)

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-

Power BI DAX Compute Latest Value for filtered rows

I have data in a Power BI DirectQuery table that looks line this:
Name Tx TxDate Other Columns
-------------------------------------------------------
A 1 1/1/2017
A 2 1/3/2017
A 3 1/4/2017
B 4 1/5/2017
B 5 1/6/2017
C 6 1/1/2017
C 7 1/2/2017
C 8 1/9/2017
In a table visual, I want to show only the latest rows for each name:
Name Tx TxDate Other Columns
-------------------------------------------------------
A 3 1/4/2017
B 5 1/6/2017
C 8 1/9/2017
However, I am using a slicer to help filter our dates and I want the latest rows that are in the sliced data. For example, if the sliced date range is 1/1/2017 to 1/2/1017, inclusive, I want to show this:
Name Tx TxDate Other Columns
-------------------------------------------------------
A 1 1/1/2017
C 7 1/2/2017
I've been trying to accomplish this by creating a measure with a DAX expression to compute the latest Date for each name, then adding a filter to only show those where LatestTxDate = TxDate, but it is not working:
LastTxDate =
MAXX(
GROUPBY(
Table,
[Name],
"Latest Tx Date",
MAXX(CURRENTGROUP(), Table[TxDate])
),
[Latest Tx Date]
)
The idea being that I calculate a new measure and then only make the row visible if the TxDate = LastTxDate.
Name Tx TxDate LastTxDate Other Columns
-------------------------------------------------------
A 1 1/1/2017 1/1/2017
A 2 1/3/2017 1/1/2017
A 3 1/4/2017 1/1/2017
B 4 1/5/2017
B 5 1/6/2017
C 6 1/1/2017 1/2/2017
C 7 1/2/2017 1/2/2017
C 8 1/9/2017 1/2/2017
This feels embarrassingly simple, but everything that I have tried doesn't work. Any help is appreciated.
I had the same problem as you and spent a whole week trying to solve it, because I couldn't found any guide on the internet. So I thought I could lend you a hand even though this was asked 9 months ago.
First things first: you need to do this with measures, as they're the only thing that is dynamically calculated within a PowerBI report. If you use a calculated column or a M formula inside the Query Editor, it will only be calculated whenever you refresh your data. They won't be affected by slicers and filters inside your report.
After trying a lot of stuff, I came up with the idea of making a measure that checks if each row is the most recent row for each group, then the measure will take a value of '1'. If it's not the last row, it will take a value '0'.
This is how I managed to do that (note: it was NOT that simple):
Build a calendar table that ranges from the minimum to the maximum of your date column. This must be done because if you use the date column itself in the next steps, the data will behave strangely. This new table's column will be used as the date slicer within the report:
Calendar = CALENDAR(MIN('Table'[TxDate]); MAX('Table'[TxDate]))
Now create a new measure in your data table that will filter the dates higher than the selected date in the report slicer and then it will find the latest date for each category or group:
Check =
VAR DateFilter = MAX('Calendar'[Date])
VAR LastDate = CALCULATE(
MAX('Table'[TxDate]);
ALLEXCEPT(
'Table';
'Table'[Name]
);
'Table'[TxDate] <= DateFilter
)
RETURN IF(LASTNONBLANK('Table'[TxDate]; 1) = LastDate; 1; 0)
If you build a table visual with all your 'Table' columns and LastDate as a measure, you will see that each row will have the most recent date of each group as a value for LastDate. Something like this:
Name Tx TxDate LastDate
---------------------------------------
A 1 1/1/2017 1/4/2017
A 2 1/3/2017 1/4/2017
A 3 1/4/2017 1/4/2017
B 4 1/5/2017 1/6/2017
B 5 1/6/2017 1/6/2017
C 6 1/1/2017 1/9/2017
C 7 1/2/2017 1/9/2017
C 8 1/9/2017 1/9/2017
It will be affected by the date slicer if you have one in your report. So the behaviour is right up to this point. Then I use the formula LASTNONBLANK() to give the date column a row context, thus being able now to compare between TxDate and LastDate and if both are the same, Check will take the value '1'.
Name Tx TxDate LastDate Check
---------------------------------------------
A 1 1/1/2017 1/4/2017 0
A 2 1/3/2017 1/4/2017 0
A 3 1/4/2017 1/4/2017 1
B 4 1/5/2017 1/6/2017 0
B 5 1/6/2017 1/6/2017 1
C 6 1/1/2017 1/9/2017 0
C 7 1/2/2017 1/9/2017 0
C 8 1/9/2017 1/9/2017 1
[Check] measure can't be used in a graph visual as of now, so we need to create another measure that will just count all rows in which [Check] takes the vale '1'. This is the simplest step as we just need a SUMX to sum all the rows of a filtered table:
Count of Tx = SUMX(
FILTER('Table'; [Check] = 1);
[Check]
)
Now you have the total count of all of your table rows with the most recent date within the range given by the date slicer. You can use this measure in a graph visual to show how many of those rows correspond to each category. For example, you are able to build a pie chart that shows how many "names" have the value "3" for Tx, how many have the value "5" and so on.
If you want a table visual that shows which is the latest date for each category, you can, instead of [Check], use [LastDate] as a measure like this:
LastDate =
VAR DateFilter = MAX('Calendar'[Date])
RETURN CALCULATE(
MAX('Table'[TxDate]);
ALLEXCEPT(
'Table';
'Table'[Name]
);
'Table'[TxDate] <= DateFilter
)
If you also want a measure that tells you which is the latest Tx for each category, you can change MAX('Table'[TxDate]) for MAX('Table'[Tx]). There are a lot of possibilities. My suggestion is that you study these formulas so you understand what they are doing in every step, so you can modify them for your needs.
You can also combine the two measures and get a table visual like this one:
Name LastTx LastDate
--------------------------
A 3 1/4/2017
B 5 1/6/2017
C 8 1/9/2017
Hope this helps you even 9 months after you asked, or maybe help other people with the same problem as we had.
I don't think you need to get that fancy. The following works for me.
LastTxDate = MAX(Table[TxDate])
Edit:
You'll want to take the max over the Tx values as well instead of listing all of them.
MaxTx = MAX(Table[Tx])
Use both MAX measures in your table instead of treating the Tx as another category.

Cannot figure out how to get RANKX to work

I have been struggling with this for the past two days. I am trying to use Power BI to create a simple report to show the top 10 occurrences of something.
I have a table called Requests that has the following:
Id Code
1 00001
2 00001
3 00500
4 00001
5 00500
6 00730
...
I am trying to count every occurrence of Code and display the top 10:
Code Count Rank
00001 3 1
00500 2 2
00730 1 3
I created a measure called Count:
Count = COUNTROWS(Requests)
I tried to create a measure for rank in the following ways:
Rank = RANKX(ALL(Requests), [Count])
Rank = RANKX(Requests, [Count])
Everything is getting the same value of 1:
Code Count Rank
00001 3 1
00500 2 1
What am I doing wrong?
You can use the built-in Top N Filter if you don't want too much hassle. Just set a Top N Filter for the Code column showing top 10 by value of Count:
I believe the reason you get all 1's is that the measure you created will just give you the total number of rows in your table each time it's used, and then you basically try to rank the same number against itself over and over, for each row.
You can get what you want by using TOPN and then RANKX. To do that, create a new table and enter this code to get the top 10 Codes from the Requests table, based on Id counts:
TOP10 = TOPN(10,GROUPBY(Requests,Requests[Code],"Count",countx(CURRENTGROUP(),Requests[Id])))
Then add a new column with this code to do the ranking:
Rank = RANKX(TOP10,TOP10[Count])
You'll get this result from your first 6 rows that you showed above:
Consider using VALUES and a variable for a calculated table in Power BI Desktop:
Top 10 Codes =
VAR MyTable =
ADDCOLUMNS (
VALUES ( Requests[Code] ),
"Occurences", COUNTROWS ( RELATEDTABLE ( Requests ) )
)
RETURN
TOPN (
10,
ADDCOLUMNS ( MyTable, "Rank", RANKX ( MyTable, [Occurences] ) ),
[Rank], ASC
)