Make first few rows unresponsive to slicer Power BI - powerbi

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-

Related

Check if value is in another table and add columns in Power BI

I have 2 tables, table1 contains some survey data and table2 is a full list of students involved. I want to check if Name in table2 is also found in table1. If yes, add Age and Level information in table2, otherwise, fill these columns with no data.
table1:
id Name Age Level
32 Anne 13 Secondary school
35 Jimmy 5 Primary school
38 Becky 10 Primary school
40 Anne 13 Secondary school
table2:
id Name
1 Anne
2 Jimmy
3 Becky
4 Jack
Expected output:
id Name Age Level
1 Anne 13 Secondary school
2 Jimmy 5 Primary school
3 Becky 10 Primary school
4 Jack no data no data
Update:
I created a relationship between table1 and table2 using the common column id(which can be repeated in table1).
Then I used:
Column = RELATED(table1[AGE])
but it caught error:
The column 'table1[AGE]' either doesn't exist or doesn't have a relationship to any table available in the current context.
There are various ways to achieve the desired output, but the simplest of them I found is to use the RELATED DAX function. If you found this answer then mark it as the answer.
Create a relationship between table1 and table2 using 'Name` column.
Create a calculated column in table2 as:
Column = RELATED(table1[AGE])
Repeat the same step for the Level column also.
Column 2 = RELATED(table1[LEVEL])
This will give you a table with ID, Name, Age, and Level for the common names between the two tables.
Now to fill those empty rows as no data, simply create another calculated column with following DAX:
Column 3 = IF(ISBLANK(table2[Column]), "no data", table2[Column])
Column 4 = IF(ISBLANK(table2[Column 2]), "no data", table2[Column 2])
This will give you the desired output.
EDIT:- You can also use the following formula to do the same thing in a single column
Column =
VAR X = RELATED(table`[AGE])
VAR RES = IF(ISBLANK(X), "no data", X)
RETURN
RES
and
Column 2 =
VAR X = RELATED(table1[LEVEL])
VAR RES = IF(ISBLANK(X), "no data", X)
RETURN
RES
This will also give you the same output.

Return Slicer's Value (trade simulator)

I work with a single table (called sTradeSim) that I have created in PowerQuery. It has 3 columns (Fund1, Fund2, Fund3), each having values from -10 to 10, with an increment of 1.
I also have three separate slicers, each created using an option "Greater than or equal to". Each slicer is having a field assigned to it - Slicer 1 = Fund1, Slicer 2 = Fund2, Slicer 3 = Fund3. Below is a screenshot of Slicer 1.
Right next to these three slicers is a table with three rows. For each row, I would like to retrieve the value of the respective slicers. So the desired result would look like:
Row No 1 = -10.00 (the value of Slicer 1),
Row No 2 = -2.00 (the value of Slicer 2),
Row No 3 = 3.00 (the value of Slicer 3).
Unfortunately, DAX formula that I have developed is always returning 3.00 (the value of the third slicer).
I have tried to find a solution on the forum and combine my SWITCH formula with ALL, ALLEXCEPT, SELECTEDVALUE etc., but it seems like I'm missing something very basic.
mHV_Trades =
SWITCH(
MAX(FundTable[FundsRanked]),
1, MIN(sTradeSim[Fund1]),
2, MIN(sTradeSim[Fund2]),
3, MIN(sTradeSim[Fund3])
)
What you are trying to do doesn't work, because essentially when you place 1 filter on any column on the table, it will filter all the rows that have that value. So, when you apply a filter fund1 = -10 it will also filter the values for fund 2 and fund 3.
You have 2 options:
Create independent tables each with values from -10 to 10
Create a table with all the combinations of -10 to 10 values for every fund.
For your example with 3 funds this works quite nicely (the table has about 10k records), all the combinations of -10 to 10 (21) to the power of 3, the problem with this solution is that depending on the number of funds you have you will run out of space quite quickly.

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
)

Creating a calculated table by passing a measure or parameter

I have a requirement where I have a data like this,
Date Name Age
1-1-2018 A 1
2-2-2018 B 1
3-3-2018 B 1
6-6-2018 C 2
7-7-2018 B 6
I am trying to give a slicer to the user to select the required number of months from the last month.
So to do that, I am using a calculated column like this:
Month Year = DATEDIFF((Table1[Date]), TODAY(), MONTH) + 1
So that changes the data to something like this:
Date Name Age MonthYear
1-1-2018 A 1 7
2-2-2018 B 1 6
3-3-2018 B 1 5
6-6-2018 C 2 2
7-7-2018 B 6 1
The user selects the Month Year from the Slicer.
For example, when he selects 2, I want to display the last 2 months records in the table.
Expected Output:
Date Name Age
6-6-2018 C 2
7-7-2018 B 6
This works for me if I hardcode it like this:
Calculated Table = CALCULATETABLE(Table1,
FILTER(Table1, OR(Table1[MonthYear] > 2, Table1[MonthYear] = 2)))
But it fails when I try to pass the value in the place of 2 dynamically through a measure using SelectedValue function.
Calculated columns and calculated tables cannot reference a slicer value since they are only computed when you load your data.
If you want to apply this filtering to a visual, I'd suggest creating a separate table for your slicer. For example, you could use Months = GENERATESERIES(1,12) and then rename the column Months as well.
Use the Months[Months] column for your slicer and then create a measure which references it to filter your table/matrix visual.
Filter = IF(SELECTEDVALUE(Months[Months]) >= MAX(Table1[Month Year]), 1, 0)
Then use that measure in your Visual level filters box:

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.