I have a colleague table (named 'colleagues') which lists all of the colleagues in the department. I have another table (named 'cases') which lists all of the cases worked by all of the colleagues. In the cases table, there is a column called 'outcome' which will be either Good, Satisfactory or Bad depending on how well the case was dealt with.
colleagues table:
cases table:
There is a one to many relationship between the colleague table and the cases table. I am trying to create a calculated column in the colleague table which will sum how many 'Good' outcome cases they had in total. This the calculated column formula I have:
CALCULATE(COUNTROWS('cases'), FILTER('cases', 'cases'[outcome]="Good")
This calculated column is just adding all of the 'Good' cases for everyone rather than just the individual colleague. See calculated column below with column name 'Good':
The expected behavior is that the column would calculate the number of 'Good' cases each colleague had. This is the expected outcome:
Create a calculated column as follows:
CountValues =
CALCULATE(COUNTROWS('cases'),'cases'[outcome]="good")
p.s. If this (or another) answer helps you, please take a moment to "accept" the answer that helped by clicking on the check mark beside the answer to toggle it from "greyed out" to "filled in".
Another solution that doesn't involve CALCULATE is
COUNTROWS( FILTER( RELATEDTABLE('cases'), 'cases'[outcome]="Good" ) )
Related
This question comes from an example that I'm trying to understand in The Definitive Guide to DAX, Second Edition chapter 4. If you want the sample Power BI file, you can download it from the website above; it's Figure 4-26 in chapter 4. Here is the DAX code:
Correct Average =
VAR CustomersAge =
SUMMARIZE ( -- Existing combinations
Sales, -- that exist in Sales
Sales[CustomerKey], -- of the customer key and
Sales[Customer Age] -- the customer age
)
RETURN
AVERAGEX ( -- Iterate on list of
CustomersAge, -- Customers/age in Sales
Sales[Customer Age] -- and average the customer’s age
)
I understand the logic behind how SUMMARIZE and AVERAGEX are used in this example, and the requirements are all clear. What's confusing to me is how AVERAGEX references Sales[Customer Age]. Since AVERAGEX is operating on the summarized CustomersAge table variable, I would have assumed that the syntax would have been something along the lines of:
AVERAGEX (
CustomersAge,
[Customer Age] -- This is the line that I assumed would be different
)
How is it that the code given in the book is correct? Does the table variable (and the summarized table it contains) somehow have pointers to the original underlying table and column names? And is that normal for writing DAX queries, to always reference the original underlying table and column names when using table variables for intermediate steps?
Yes, the columns have what's known as data lineage. Sometimes you even have to restore lineage if it gets lost. You can read more about it here: https://www.sqlbi.com/articles/understanding-data-lineage-in-dax/
Lars, To the best of my understanding this is how I can explain it.
Creating a variable doesn't create a table that is added to the model. You can think of variables as steps or placeholders of a series of DAX expressions.
And so in the case of the SUMMARIZE used in the CustomerAge variable in this code, you'd see that the actual columns in the model were what was referenced in the arguments of SUMMARIZE. So when you perform calculations on that variable, the columns you can access are the actual columns in the model rather new columns.
What the variable has done is to help you break down the process of writing the calculation and make it less complex.
The code you wrote, as what you expect, would have been valid if in the CustomerAge variable, we created a new column, say Age * 2, and needed to perform the average over that. Then in that case that new column isn't part of the model, thus we'd reference it like you wrote.
I just got my copy of the book but I hope this helps a bit.
I have the following Power BI table example for an operating expense report that uses a slicer to filter the first column named "Actual". This is to see the operating expenses for one month compared to the budget figures for the year. It also compares the year-to-date and annual figures. How can I create dynamic columns that change based on the slicer selection? These additional columns are not shown in the pic below but included in the last pic. The Budget column below was just created as an example to show what it should look like.
I set up a star schema with several tables shown below. There's only one expense fact table used and the slicer only works for the first column as previously stated but I need all the other columns to use different parameters and adjust based off what's selected in the slicer. The last image is an overview of the info and the parameters for each column. I tried creating new columns with measures for the budget to see if I can get that going but can't figure out how to make it adjust with the slicer selection.
I'm not sure if I should be using separate queries for each column or can this be done using the one expense table. Hope this isn't too confusing. Please let me know if more info is needed.
If I understood what you wanted correctly I think I solved your problem.
I was able to create the following:
I did not use all values since I did not want to type everything, if you provide some test data it is easier to replicate you dashboard.
This matrix (so not table) allows you to filter for Date (if you so desire, you can always show all date's in the matrix) Book and AccountTree.
The way this is done is by putting the address column in the ROWS for the matrix, Putting the Date column in the COLUMNS of the matrix and putting your values (actual, budget, variance) in the values of the matrix.
For the date is used days, since it was easier to type. You can always use weeks, months, quarters or years.
For this to work you have to create the following relationships:
Hope this helps.
If not, please provide test data so it is easier to try and solve your problem.
After creating a table on the report tab I need to count the number of rows that are in the table, this seems SUPER easy, right? I agree, it should be.
Well, if I add in another field into the "values" list and try to make this a "count" or "count (distinct)" type then the table's filters seem to be affected and I get different information in the table than what I had seen previously. WEIRD.
So, I need a way to count the number of rows in a table on the report tab without adding in another column to the table.
Any suggestions would be appreciated. Can provide more information if needed!
CreatedCounts =
CALCULATE(COUNT('Bugs - All history by month'[Work Item Id]),
DATESBETWEEN('Bugs - All history by month'[Created Date],
MIN('Bugs - All history by month (2)'[Iteration Start Date]),
MAX('Bugs - All history by month (2)'[Iteration End Date])))
This is what I came up with, and it works exactly as I want from what I can tell, but I don't really 100% understand WHY it works, so if anyone can provide an explanation (or better solution!) please let me know, thanks!
I am using the AdventureWorks2016 data warehouse database. I created a measure named 'Total Sales Rank', which can be seen below. I am simply trying to rank each product according to sales (internet sales). The 'Total Sales' column in the table below is a measure (Sum([SalesAmount])) which sums all sales. I cannot figure out why RankX is returning 1 for each product. There are no filters in place. All the tables are properly related.
By the way, there are other questions somewhat like this but different enough where the answers do not help this situation.
You need to use ALL('Product') instead of just 'Product'.
Since you have products as filters (yes, you do!), for each row in your report RANKX "sees" only one record (for the product of the row). That's why you are getting "1"s. Instead, in each record you need to "see" the entire table, so that RANKX can compare multiple rows. This is accomplished by using ALL() function (or ALLSELECTED, etc).
This article might help you further:
Using RANKX
I'm new to the DAX world and I just faced a conceptual misunderstanding.
While I was trying to debug a measure, I found a unexpected (for me) behaviour with the ALL function. I wrote a simple measure COUNTROWS(ALL(Table)) but to my surprise, when I sliced the data by a column, the same number did not appear in each cell in the values field, ignoring the filter context in the way I was expecting:
So, what am I missing?
Since there are no BUYs on e.g. 01/08/2017, it's not even evaluating the measure. Since that cell doesn't correspond to any rows in the original table, it's not doing any computations.
If you had a BUY and a SELL on the same day, it should show up in both columns on that day.