Comma delimiter filter in Power BI - powerbi

So I have a column like this in Power BI matrix
Country
India
India, Pakistan
Myanmar, Bhutan, India
I want to add a filter, where if I choose India, all the above three entries should return.
Currently my filter looks like this:
India
India, Pakistan
Myanmar, Bhutan, India
I want it to look like this
India
Pakistan
Myanmar
Bhutan

You can create a "NEW TABLE" from the modelling menu and use the following DAX query;
FilterTable =
VAR tmp =
ADDCOLUMNS ( Sheet1, "ItemPaths", SUBSTITUTE ( CONCATENATEX(Sheet1, Sheet1[Column1]), ",", "|" ) )
RETURN
DISTINCT(SELECTCOLUMNS (
GENERATE (
tmp,
ADDCOLUMNS (
GENERATESERIES ( 1, PATHLENGTH ( [ItemPaths] ) ),
"Items", TRIM(PATHITEM ( [ItemPaths],[Value], TEXT ))
)
),
"Countries", [Items]
))
Replace Sheet1 and Column1 with your table and column name...

Here are the steps which i followed to solve the issue
created a duplicate table as my source table
Deleted all the columns except the one which i wanted to keep a filter for
Duplicated that column and separated it row wise by removing the delimiter
Made a relation b/w my primary table and this newly created filter table
(the reln was showing as many to many but go ahead with it)
Apply in the filters, and the same solves the above issue.

Related

How to pass multi column to remove filter using All in Dax formula in power bi

I have one requirement where I need to overwrite the filter context using All key word in Power bi Dax
In the table there are two value columns: Sales and Purchase and one date column .My calculation is based on these column
Simply dividing Sales by Purchase .But there are many attributes column with text values I want to remove the filter context for Purchase calculation .My data will look like below
My Dax formula looks like this:
measure_name =
CALCULATE (
SUM ( table_name[Sales] ),
YEAR ( table_name[Date of entry] ) = MAX ( YEAR ( table_name[Date of entry] ) )
)
/ CALCULATE (
SUM ( table_name[Purchase] ),
YEAR ( table_name[Date of entry] ) = MAX ( YEAR ( table_name[Date of entry] ) ),
ALL ( table_name[Attribute1] ),
ALL ( table_name[Attribute2] ),
ALL ( table_name[Attribute3] )
)
here I have put all attributes in the dax simply putting coma which will throw error
Any one can help me how to write this .Only for purchase(denominator calculation ) I want to remove the filter for any number of attribute columns.Or is there any other way to do this calculation .This data is dummy data .In future if we are getting more attributes , can we pass one list for this All parameters .So that user will just update in the excel all attributes and when it get refreshed , these parameter values to pass in to this All parameter automatically..?

Left Joining Tables in Power BI based on multiple columns with duplicate values (DAX)

I am trying to join 2 tables (left join) in Power BI using DAX, and I keep on encountering errors.
The dummy table structure and desired outcome are below:
How would I join the two tables where the Application and Business Unit should match in Both Tables and considering that Table A & B contain duplicate values?
One of the statements I have tried is below, however I keep on encountering errors regarding duplicate values and I am not sure how to proceed. I can't use Power Query unfortunately, this has to be in DAX.
TABLE =
GENERATEALL (
Table_A,
CALCULATETABLE (
ROW (
"New vs Old", VALUES (Table_B[New vs Old]),
"Project", VALUES (Table_B[Project])
),
TREATAS ( ROW ( "Business Unit", Table_A[Business Unit] ), Table_B[Business Unit] ),
TREATAS ( ROW ( "Application", Table_A[Application] ), Table_B[Application] )
)
)
Thank you
Use NATURALLEFTOUTERJOIN Like this:
Table =
var TABLE_A = SELECTCOLUMNS(
{
("A","BU1","2022-10",100),
("B","BU2","2022-11",200),
("B","BU3","2022-10",100),
("C","BU1","2022-11",400),
("D","BU2","2022-12",50)
},"Application",[Value1],"Business Unit",[Value2], "Month",[Value3], "Cost",[Value4])
var TABLE_B = SELECTCOLUMNS(
{
("A","BU1","Project 1","New"),
("B","BU2","Project 2","New"),
("B","BU2","Project 5","Old"),
("B","BU3","Project 3","Old"),
("C","BU1","Project 1","Old"),
("D","BU2","Project 4","New")
},"Application",[Value1],"Business Unit",[Value2], "Project",[Value3], "New vs Old",[Value4])
return NATURALLEFTOUTERJOIN(TABLE_A,TABLE_B)
If your column names or data types don't match you'll need to CONVERT and rename them before joining.
You need to create a Calculated Column that introduces a Union between "Applications" and "Business Unit", in both tables, then introduce tge relationship with this column created. It’s like creating a Primary Key.

How to count the number of rows containing a specific phrase in PowerBi?

I'm using power bi.
I have a columns name "status"
and I want to count the number of rows in this column that contains the string "cnn".
Sometimes the string in the middle of the phrase sometimes in the end.
Thanks in advance
CONTAINSSTRING seems like this key here.
Try something like this:
COUNTROWS (
FILTER ( Table1, CONTAINSSTRING ( Table1[Status], "cnn" ) )
)
If you want the number itself for a KPI visual, you wrap the count statement that takes a particular column in a calculate and then use the CONTAINSSTRING as a filter for that particular column
CountCNN=CALCULATE(COUNT(Table["status"]),CONTAINSSTRING(Table["status"],"cnn"))

I need to filter my table in Power BI with multi filter with "AND" logic

I'm new to Power BI and DAX Language.
I have a table that contains names, skills, and assessments. For example, like this:
In Power BI, I need to have two filters one by skill and other by assessment, to show me the name that I want:
For example: If I filter by skill =Python and assessment between 2 and 3, the result should be Name =A, not A, and C, what happened is output is considered that I have at least one result, that is Python.
Conclusion: I want to filter by skill (or all skills) AND by level (which can be ONE level or an interval). For default Power BI shows me OR not AND.
I try this DAX formula:
name with all skills =
CALCULATE (
COUNTROWS (
FILTER (
ADDCOLUMNS (
VALUES ( Assessments[corp id] );
"Skills"; CALCULATE (
COUNTROWS ( VALUES ( Assessments[skill] ) );
CALCULATETABLE ( Example )
)
);
[Skills] = COUNTROWS ( ALLSELECTED( Assessments[skill] ) )
)
);
ALLSELECTED (Assessments[skill] )
)
When you filter on multiple columns, Power BI combines the conditions using AND logic. Obviously, you don't want to use AND for the same column since, for example, Name can't be A and B simultaneously, so OR is used when selecting multiple values in a single column.

Power BI - Running total column with many filters

I have table "Sales" (fields: Product, Country, Date, Sales) with monthly sales across many products and countries. Also I have tables with calendar, list of products, list of counties that are linked with this table. I want to add column to "Sales" with running total sales across each Product/Country, see the field with desired result "Running total".
I tried to use
YTD = TOTALYTD(SUM(Sales[Sales]); Calendar[Date]) but it didn't work. I think I need to use filters in TOTALYTD function, but I also didn't manage to understand how. Can you suggest to me a right solution to my case?
Table "Sales"
I was suggested to use this code
Column =
SUMX (
FILTER (
Sales,
Sales[Product] = EARLIER ( Sales[Product] )
&& Sales[Country] = EARLIER ( Sales[Country] )
&& Sales[Date] <= EARLIER ( Sales[Date] )
&& YEAR ( Sales[Date] ) = YEAR ( EARLIER ( Sales[Date] ) )
),
Sales[Sales]
)
It worked.
I partially coped with my issue by creating set of measures for each combination of product and country:
A_US = TOTALYTD(SUM(Sales[Sales]);'Calendar'[Date];FILTER(All(Sales);Sales[Product]="A"&&Sales[Country]="US"))
A_Canada = TOTALYTD(SUM(Sales[Sales]);'Calendar'[Date];FILTER(All(Sales);Sales[Product]="A"&&Sales[Country]="Canada"))
and so on. But what if i have 100 products and 30 countries? I think I need to create a column "Running total" in "Sales" that calculates running total for each product and aech country.
The problem of the TOTALYTD function is that it takes only one filter.
The tricks is to use the filter function like you do on the second response.
To use only one column for all product and country you have to get the context of the current row .
To achieve this you have the function earlier in dax.
Here the documentation about earlier : https://learn.microsoft.com/en-us/dax/earlier-function-dax
The column need to be build with this expression :
TOTALYTD(SUM(Sales[Sales]),'Calendar'[Date],filter(Sales,and(Sales[Country]=EARLIER(sales[Country]),sales[Product] = EARLIER(sales[Product]))))