I'm having some issue regarding the filtering in Power BI/DAX. I have an example below. What I'm trying to achieve is to write a measure to filter out the rows where column 1 is "A" and Column 2 is "aa" and then sum the total of the rest of rows.
Thanks in advance!
You can use CALCULATE like this:
Sum of Filtered Values =
CALCULATE(
SUM('DataTable'[Amount]);
'DataTable'[Column1] = "A";
'DataTable'[Column2] = "aa"
)
The two rows below SUM() are filter arguments in the CALCULATE function. Each of the arguments removes all other filtering on their respective columns and then CALCULATE generates a table where the two filter arguments are combine with a logic AND, hence CALCULATE will SUM all [Amounts] where [Column1] is "A" and [Column2] = "aa".
This is basic DAX workflow. Read about CALCULATE, understand how the filter functions work (and their inherrent pitfalls), and learn to love it.
Related
im struggling with the following problem. I have categorical variables and and Amount column. What I want to do is, to write a dax measure which calculates the moving/rolling Sum, like you see in the third column "Dax Measure". Did not find any inhouse Dax function for that.
moving sum
EDIT:
Result Table with Dax Measure
Source Table
This will work if the category is always sorted ASC.
Dax measure =
VAR _currentRank = RANKX(ALL('Table'[Category]),CALCULATE(MIN('Table'[Category])),,ASC)
RETURN
CALCULATE(
CALCULATE(SUM('Table'[Amount]),TOPN(_currentRank,VALUES('Table'[Category]),MIN([Category]))),
ALL('Table'[Category])
)
Using RANKX to define the other and aggregating for all the item before it.
In Power BI I'm trying to combine and get the unique values of pages in a new table. I need to filter one of the columns, based on a value of another column in that same table. So i want to do something like
FILTER(VALUES('Table1'[URL]), 'Table1'[Correct] = "Yes"
But it's not working, because I can only filter based on calculations, not columns.
The complete line that I have now and is working is:
All pages = FILTER(
DISTINCT(UNION(VALUES('Table1'[URL]),VALUES(Table2[Complete URL]),
VALUES('Table3'[URLs]))), [URL] <> BLANK())
How can I add a filter to one of those tables, based on another column of that table?
You can just replace FILTER with CALCULATETABLE:
CALCULATETABLE(VALUES('Table'[URL]), 'Table'[Correct] = "Yes")
If you need to count the values, just wrap it into COUNTROWS:
Measure = COUNTROWS( CALCULATETABLE(VALUES('Table'[URL]), 'Table'[Correct] = "Yes"))
I am creating a report in which there is one table which should shows top 3 Employee Names and there respective sale amounts based on there Sales Amount.
When TOPN is used alone, I get correct output in form of new table. But its without sale Amount. However when TOPN is used along with CALCULATE function, I get all the Employee Names, which is incorrect
DAX query that works well but has only one column:
TOPN(3,values(Sale[Employee Name]),CALCULATE(sum(Sale[Total Excluding Tax])))
DAX Measure which is not filtering the top 3 rows :
TopN = CALCULATE(SUM(Sale[Total Excluding Tax]),TOPN(3,values(Sale[Employee Name]),CALCULATE(sum(Sale[Total Excluding Tax]))))
Where am I going wrong, please let me know. Screenshot of both the queries is as follows:
since your first DAX query is working:
TOPN(3,values(Sale[Employee Name]),CALCULATE(sum(Sale[Total Excluding Tax])))
How about just creating a simple SUM-measure and adding it as another column, next to the TOPN?
Sum sales = SUM(Sale[Total Excluding Tax])
It should work, since your TOPN is calculating correctly. Or am I misunderstanding the question?
I'm trying to remove filters from a calculated column with ALL(), and the measure seems to ignore it
"Class" is a calculated column.
I want to add measure always showing 100 (the grand total)
There are outer filters on the table so I don't want to use ALL(TABLE)
However, CALCULATE(SUM([Total Sales]), ALL(Product, Class)) doesn't work.
You can use the ALL() function on a list of columns like this:
CALCULATE ( SUM ( Table1[Sales] ), ALL ( Table1[Product], Table1[Class] ) )
This should work fine even if Class is a calculated column.
Did anyone know how to convert RunningCount into Power bi Dax ?
I test on RunningTotal, Rankx but seems not working.
The [year] is just a text column not in Datetime format.
I still new on this and not sure if my explain is good enough or not. Sorry for any inconvenient cause.
I try to create measure/ calculation column in power bi from the formula below.
I need the count by Year, break by column like product, customer, rate and category.
Before I get into my solution, there are a few assumptions I had to make:
The "Year" column is supposed to be years, and not five digit numbers. So in my data I dropped the second "2" in each (i.e. "20213" -> "2013").
You may have another column in your data to break ties, but given the data you provided, there is no way to rank the first and third lines (they both have product ABC and year 2003).
Given those assumptions, here is my solution...
First, here is what my data looks like. I added an ID column so that we can see every row, even duplicates.
From there, you can simply add a new column with the following formula.
Running Count =
COUNTROWS(
FILTER(
'Data',
[ProductName] = EARLIER([ProductName]) &&
[Customer] = EARLIER([Customer]) &&
[Seller] = EARLIER([Seller]) &&
[Year] <= EARLIER([Year])
)
)
The EARLIER function is being used to specify a ProductName, Customer, etc. from the row of the table to be used in the filtering of the data. Once we have the data filtered, we can simple count the number of rows.
The final result looks like this. As noted in my second assumption, there is no way to break ties, so my numbers are slightly off from what you have in your screenshot