DAX Power BI Conditional filtering - powerbi

Sample file
The column c of a table T can have the values 1, 2 or 3. I want to filter the table such as when the user selects value 1 nothing is filtered. When the selected value is 2 then show only the rows with the c column containing the value 2 or the value 3 not the value 1 and finally if the selected value is 3 then show only those rows containing 3 in the c column. The slider or a plain filter must be single selection not multi because otherwise it would violate one of the user's business rule.
Selected Show
1 all rows
2 rows with 2 or 3
3 only rows with 3
I tried to create columns and to create measures but I can't get anywhere. Any directions?

I agree that the disconnected table might be the best workaround here. I hope this solution will work with your actual file.
Step 1 - create a new disconnected table
FilterC = DISTINCT(T[c])
Step 2 - make sure that your slicer is sourced from the new table (FilterC)
Step 3 - create a measure that will return 1 for rows that we want to see, and 0 for rows that we want to hide:
mFilter =
var Filter_C = SELECTEDVALUE(FilterC[c])
var Row_C = SELECTEDVALUE(T[c])
return
SWITCH(
Filter_C,
"1", 1,
"2", IF(OR(Row_C = "2", Row_C = "3"), 1, 0),
"3", IF(Row_C = "3", 1, 0)
)
Step 4 - add this measure to the table, or even table filters will suffice.
Step 5 - start switching the values!

First you need to set it to a number column.
Second, you could write complex measures and use a disconnected table, but the best option is to use the correct slicer. There is a Greater than or Equal To option.

Related

Power BI - CRUD analysis how?

I have 2 data sources - Source 1 ( SharePoint List ) & Source 2 ( Cloud Source ). I bring both into Power BI. They each have a key to identify a unique instance of a record.
In Power BI I have been asked to identify the New Inserts / Deletes and Updated Records.
So is there an easy way of doing this?
Table 1
Key Column 1 Column 2 Column 3
Table 2
Key Column 1 Column 2 Column 3
You can use Merge queries transformation in Power Query Editor to do that. Left Anti and Right Anti join kinds will give you the rows that exists only in the first or second data source. Inner will give you the rows that exists in both (based on their key value) and later you can compare the other columns to decide are they modified or not.
Lets assume there are two data sources, as follows:
Source 1
Key
Column 1
Column 2
Column 3
1
initial value
initial value
initial value
2
initial value
initial value
initial value
3
initial value
initial value
initial value
Source 2
Key
Column 1
Column 2
Column 3
1
initial value
initial value
initial value
2
modified value
initial value
initial value
4
initial value
initial value
initial value
1 exists in both sources and is not modified;
2 exists in both sources but is modified (Column 1 has different values);
3 exists only in Source 1;
4 exists only in Source 2.
In Power Query Editor, in Home tab of the ribbon, in Combine group, click on Merge Queries -> Merge Queries as New, select Source 1 as the first source, Source 2 as the second source, set the join kind to be Left Anti and make sure Key columns in both sources are selected:
This will give you the rows, that exists only in Source 1, i.e. only 3 (and remove the columns from Source 2 there, because they are not needed):
Do the same merge again, but swap the sources:
to get the rows, that exists only in Source 2, i.e. 4:
And then do it again, but this time set the join kind to be Inner:
Click on the button in the header of Source 2 column and add all the columns except Key and add a conditional column (Add Column -> General -> Conditional Column) as follows (note, that the screenshot is incorrect - the third comparison should be between Column 3 and Source 2.Column 3):
It will tell you is this row modified (2) or not (1):
If you want you can click the button in the header of the custom column and filter the result to show only the rows, where the value is Modified, which will leave only 2 in the result:

Trouble counting occurence of values between 3 connected columns

I am having tremendous difficulty in getting around a Power BI function.
I am super new to Power BI and I've learned a lot already but I'm still confused about some topics.
I uploaded some data from excel into my Power BI applicaiton and it had some data as such.
Returns (Tab 1)
Order ID (column 1)
1
2
3
Orders (Tab 2)
Order ID (column1) Product ID (column 2)
1 100
1 101
2 101
3 101
3 100
4 100
Product (tab 3)
Product ID (column 1) Product(column 1)
100 Table
101 Chair
What I am trying to do is figure out the top 5 products that are returned.
I know the data above only has 2 products, but the actual data in my Power BI file has many more.
I basically want to take the Order ID from tab 1, compare them with the multiple product ID's each order has in tab 2 and then compare that with the product names in tab 3 to figure out the top 5 products that are returned.
I've already establihed all the relationships and linkages.
I've tried to use the "Top N", count distinct and sum functions but I was unable to get the filteration mechanism to work properly.
I kept on getting the total number. I also tried using matrix but that didn't work either.
You can access the file with this[link][1] in case anyone wants to look at the data I am working with to get a better idea of my problem. Page 3 is where majority of my attempts have been at trying to solve the problem.
I'm basically trying to find the top 5 most returned products, ideally in a graph and maybe using a slicer.
https://1drv.ms/u/s!AnMQvyG3G5w6iTcKZUaBvA_oLalv
That link above leads to the download page to downloads the PBIX file I am working with.
I would greatly appreciate any help.
First create a Table using this below DAX-
minimum_return_show =
VAR return_per_product_name =
SUMMARIZECOLUMNS(
Products[Product Name],
"return", count(Products[Product Name])
)
var top_n =
MINX(
TOPN(
5,
return_per_product_name,
[return]
),
[return]
)
return
TOPN(
1,
FILTER(
return_per_product_name,
[return] >= top_n
),
[return],
ASC
)
Now create a Measure as below-
product_count =
if(
count(Products[Product Name]) >= max(minimum_return_show[return]),
count(Products[Product Name]),
0
)
Finally create your bar chart using column product_name and measure product_count. Apply filter on product_count with greater than 0 and hopefully you will get your desired 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.

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

PowerBi Change Card values to previous month Value if Current month value is not available

I am working on powerbi, using data over 2 months period, writing dax queries and trying to build reports.
I am trying to show the monthly values in cards visuals.
And i am able to do it using the measure below.
Sample Data:-
PlanPrevMon = CALCULATE([PlanSum],PREVIOUSMONTH('Month Year'[Date]))
CustomKPI = IF(ISBLANK([PlanSum]),"No Data Available ",[PlanSum])&" "&IF([PlanSum]=[PlanPrevMon],"",IF([PlanSum] > [PlanPrevMon],UNICHAR(8679),UNICHAR(8681))&IF([PlanSum]<=0,"",""))
But here i don't want user to choose the month values from slicer.
I would like to show the card values as
if current month value is not available then it should compare with the previous month value automatically as shown in an image below
For example, Apr-2017 value is not available; in this scenario I would like it to compare with the Mar-2017 value.
If Mar-2017 value also not available, then the previous month value and so on
Edit
I tried what #user5226582 suggested but still getting wrong values as below image.
As you can see i restricted to crosscheck whether the value is getting in a right way of not.
But not.
This is the measure i have used as #user5226582 suggested.
c =
var temp = 'Revenue Report'[Date]
return CALCULATE(LASTNONBLANK('Revenue Report'[Planned Rev],
'Revenue Report'[Planned Rev]),
ALL('Revenue Report'),
'Revenue Report'[Date]<=temp)`
Can you please correct me if i am doing anything wrong
Does this help...?
To get the PlanPrevMon column to show like this, I first created a new index column:
id = COUNTROWS(FILTER(Sheet1, EARLIER(Sheet1[Date],1)>Sheet1[Date]))
Then I used the index to help create the PlanPrevMon column in two steps:
Step 1: I made one column named PlanPrevMon1.
PlanPrevMon1 = SUMX(FILTER(Sheet1,Sheet1[id]=EARLIER(Sheet1[id])-1),Sheet1[PlanSum])
Step 2: I made another column named PlanPrevMon.
PlanPrevMon = if(ISBLANK(Sheet1[PlanPrevMon1]),
if(Sheet1[id]=1,0,CALCULATE(LASTNONBLANK(Sheet1[PlanPrevMon1],Sheet1[PlanPrevMon1]),ALL(Sheet1),ISBLANK(Sheet1[PlanSum]))),
Sheet1[PlanPrevMon1])
For the card, I used this measure:
Card = CALCULATE(LASTNONBLANK(Sheet1[PlanPrevMon],1),FILTER(Sheet1,Sheet1[id]=max(Sheet1[id])))
I hope this helps.
You can use LASTNONBLANK DAX function.
Example data:
a b
1 1
2
3 2
4
5 3
Calculated column:
c =
var temp = Table1[a]
return CALCULATE(LASTNONBLANK(Table1[b],Table1[b]),ALL(Table1),Table1[a]<=temp)
Results in:
a b c
1 1 1
2 1
3 2 2
4 2
5 3 3