Why is RankX always returning a 1? - powerbi

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

Related

How is it possible for DAX syntax to reference the original table name when using table variables?

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.

How do I create measures that are not aggregated data?

I have a simple table which has a number(say sales) as one column which describes the sales done for a product. I also have ~25 products.
Now, I want to import this sales as a measure into cube. However, i am not sure what it's type and sql parameters should be. Setting the type to 'number' and sql to 'sales' gives error such as column "plan.sales" must appear in the GROUP BY clause or be used in an aggregate function.
What am I doing wrong?
Сould you please share the env vars, the cube.js file, and the data schema?
I also recommend you follow up through this guide. I think it might help.
I think you should add the sales column as a dimension, then you can define the aggregated measures like average sales or total of sales using the number of sales dimension.

SUM LOOKUPVALUE with condition

I would like some help figuring out some DAX in Power BI.
I’ve included two screenshots of some mock tables I created in Excel to provide context. I have one master table called Financials which has all of the financial transactions associated with a claim number. I have another table called Test which is built from the Financials table.
The Test table contains the distinct values of claim numbers since in the Financials table a claim number can have multiple transactions. In the event that a claim number has multiple transactions, it would be on separate rows, as shown in the screenshot. Below is the formula I used to create my Test table with distinct claim numbers.
Test Table = distinct(Financials[Claim Number])
My Request
In reference to the screenshot for the attached Test table, I would like to create two columns titled Investigation and Settlement. In plain English, I would like the DAX formula to sum the check amount if the payment type in the Financials table is Investigation. The same logic would apply when payment type is Settlement. In the end I will have one column for Investigation and one column for Settlement, as shown in the Test table image.
I’m thinking something like: if lookupvalue from payment type in Financials = 'investigation' then sum check amount. Also, not sure if I would use SUM or SUMX.
The Test table will then have one row for each claim and it will have summed up all of the investigation payments and all of the settlement payments for the related claim from the Financials table.
Thank you for any help. I'm sure it's easy yet I can't figure it out!
Create a new table and write this code:
test_table =
SUMMARIZE(
financial_table,
financial_table[Claim Number],
financial_table[Claim Status],
financial_table[Date Closed],
financial_table[Legal],
"Investigation", CALCULATE(
SUM(financial_table[Check Amount]),
financial_table[Payment Type]="Investigation"
)+0,
"Settlement", CALCULATE(
SUM(financial_table[Check Amount]),
financial_table[Payment Type]="Settlement"
)+0
)

Can a measure be created in Power BI that references two tables that share no relationship?

I'm trying to create a matrix table in Power BI to display the monthly rent projections for a number of properties. I thought I could simply create a measure that summed the rent from one table and then displayed it by month based on start and end date conditions, but it's been a while since I created any measures and I had forgotten that there needs to be a relationship between columns, among other things.
Data Model
A site can have more than one lease associated with it and a lease can have both car-parks and floors associated with it, of which there can be multiple.
In addition to the tables in the linked image, once I had sorted out what I thought would be the easy step I was going to add another table which includes the estimated percentage rent increase and the period in which the increase will occur.
I started out by trying to create a measure along the lines of the following:
Matrix Test =
IF (
HASONEVALUE ( Period[Month] ),
IF (
Period[Month] >= Leases[Custom Start Date],
SUM ( Floor_Rent[Annual Rent] ) / 12,
0
),
0
)
This would need to be expanded upon because the end date of a lease would also need to be taken into consideration.
As well as forgetting about the relationship requirements, I've forgotten how to deal with the issue of narrowing down to a single value within a column.
The result is supposed to be something that looks like this:
The blanks indicate a lease that starts in the future or ends within the time-frame displayed.
When I try linking the Leases table and the Period table on Leases[Start month for current term] and Period[Month] all I can get to is a table that shows the rent amount in the month the lease starts.
Is what I'm trying to achieve possible? If so, how do I accomplish the desired result?
Link to .pbix file
Solution
The direct answer to the title question is probably 'no', but while trying to figure out how I could use Pratik Bhavsar's LOOKUPVALUE suggestion I had a thought and performed a clumsy google search - power bi create table for each value in column - and found this post. By meddling with some of the DAX in said post I was able to come up with the following:
Test Table =
GENERATE(
SELECTCOLUMNS(
VALUES(Leases[Lease ID]),"Lease ID",[Lease ID]
),
SELECTCOLUMNS(
VALUES(Period[Month]),"Month",[Month]
)
)
The result is a table with each Lease ID mapped against each Month. I can't claim to understand exactly how the functions work, and it's not the outcome I thought I needed, but it allows me to achieve exactly what I set out to do.
I've accepted Pratik Bhavsar's answer because it effectively accomplishes the same thing as the work around I implemented. Pratik's solution might be better than what I eventually landed on, but I need to have a closer look at how the two compare.
The following DAX will give you a table with all buildings mapped against all rows in the period table, eliminating the requirement of a relationship.
SiteToPeriod =
CROSSJOIN(
SELECTCOLUMNS(Sites1, "Building name/label", Sites1[Building name/label]),
Period
)

Is it possible to use a slicer as a parameter to a DAX Summarize function?

I have a FactLosses Table, and a DimAccumulation table. I have brought them into PowerBi and I have placed a slicer to choose which accumulation zones i am interested in.
Once the user has selected the zones, i want to perform a group by year on the losses and sum the losses into year buckets. But only on the data that applies to the zones the user picked.
I am using the following DAX code to do the group by like so...
Table = SUMMARIZECOLUMNS(FactForwardLookingAccumulation[Year], "Losses By Year", SUM(FactForwardLookingAccumulation[Net Loss Our Share Usd]))
The problem is the new table always produces the same result. i.e When i make changes to which accumulation perils should be included it makes no difference to the summation. (it is summing the entire table)
I'd like to use the slicer to filter the fact table and then have the DAX query run on the filtered list. Is this possible?
If you want these tables to be responsive to filters or slicers on your report, then you can't write these as calculated tables that show up under the Data tab since those are computed before any filtering happens.
To get what you want, you have to do everything inside of a measure, since those are what respond to slicers. If you're looking for the max loss year once the grouping and summing are completed, you can write a measure along these lines:
Year Max =
VAR CalculatedTable = SUMMARIZECOLUMNS(FactForwardLookingAccumulation[Year], "Losses By Year", SUM(FactForwardLookingAccumulation[Net Loss Our Share Usd]))
RETURN MAXX(CalculatedTable, [Losses By Year])
Writing it this way will allow the calculated table to respond to your slicers and filters.