DAX Lookupvalue - powerbi

I've searched and searched.
I have a table with Zip code, city, county, state.
I have another table that I want the zip code to populate based upon the city column and the state column.
The tables are unrelated and I have been able to populate the state using Lookupvalue
City, State, Zip
Since states have cities with common names (Arlington, VA vs. Arlington, TX), I'm trying to use Lookupvalue to find the zip by city then by state. So the attached image would go:
Zip Code 2 =
LOOKUPVALUE('Current States Single County'[zip],'Current States Single County'[city], 'Zip Code_retest'[city],'Current States Single County'[state],'Zip Code_retest'[statename])
But I get the "A table of multiple values was supplied where a single value was expected error."
Can Lookupvalue perform this? The code looks like I should be able to put multiple criteria. If not, what is recommended?
Thanks.

You have to use your LOOKUPVALUE expression as a calculated column and not as a measure since the formula requires a row context.

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.

Power BI Combining Columns and use lookup

I have two reports: first report has a column with sponsor names (name: Sponsor) and the other report also has sponsor names but written differently (name: Companies). Example:
**Sponsor**
Apple
Target
Amazon
IBM
Samsung
**Sponsor (Other)**
Apple Inc
Target LLC
Amazon Marketplace
IBM Computers
Samsung Company
I have appended these two columns so that they are in the same report called Sponsor_All, column names the same as above. What I would like is to create a new column where it would pull in the names from the sponsor column and change the name of the Sponsor (Other) column based on a lookup table so that all of the names are labeled like the sponsor column. Hope thats makes sense.
It looks like what you need is conditional column of some sort.
First thing I would recommend, when you append the two tables, if column names are the same it will append values from both tables into one column, instead of having two columns with different name and a bunch of null values. After that you can either add Conditional Column and hard code the values (so it will be something like: if Sponsor starts with 'Apple' then 'Apple' etc.).
This is not the best approach though, cause you will have to maintain these conditions manually. Better if there's some sort of pattern that you can notice, e.g. in your example I see that to get from Sponsor (other) to Sponsor you just need to extract the first word. If this is the pattern you always have you can use Custom Column and use formula to extract only the first word.
Lastly, if you already have some sort of lookup table you can merge it (after appending together Sponsors and Sponsors(Other) into one column) and use fuzzy lookup option. In all honesty though I never used it and not sure how good it is. If it gives you good enough result you can "clean it up" in next step with custom or conditional column.

Having one column dependent on the order of another column in Amazon QuickSight

Here is the simple table data I have:
What I want:
For each business, I am only interested in it's LATEST Profit% in QuickSight table format (or any other visual type honestly and not just table)
What I have tried:
I put this table into QuickSight's Table visual type and put the "Business Name" into "Group by", "Timestamp" and "Profit%" into "Value". Fortunately, I can select only the max value of the Timestamp column (which is exactly what I want!) as you can see in the image below:
The first two columns are exactly what I want however if you look at the "Profit%" column, you can see that I am getting a "Sum" value. But I just want the respective value associated with that respective "Max" Timestamp only! For example for the "Business-1" row, the expected value in the Profit% column is 25.
Possible solutions I have tried:
i. Using a calculated field using the lastValue function - Unfortunately, this function is not supported in the region I am using QuickSight. If it was supported, my issue would have been resolved.
ii. Using a calculated field called rank using the function rank and then using a filter for this column. This has not worked out as yet. Suggestions are welcome if it is actually possible using this logic.
I think you will be able to get the desired result by using the maxover function and an ifelse function
ifelse(Timestamp=maxover(Timestamp,[{Business Name}],PRE_FILTER),{Profit%},0)

Power BI How to Sum Based on If a Column Contains String from Other Column

I have an Entity column with one row per entity. This table has three columns: Entity ID, a Descriptor, and a Metric. The Descriptor is a concatenation of numerous codes and I would like to see the metrics broken down by code.
I originally just split the Descriptor column into numerous rows but that led to some data relationship issues so I'd like to do it without splitting the Descriptor column.
I tried doing the following DAX formula but it resulted in an error stating "the expression contains multiple columns, but only a single column can be used in a True/False expression that is used as a table filter expression"
Desired Output Metric = CALCULATE('Metric',CONTAINSSTRING('Entity Table'[Descriptor],'Code Table'[Code]))
Ultimately I'm not even sure I need this as a column, and it may be better as a measure...
Any help would be appreciated. Thank you!
You can get around "the expression contains multiple columns, but only a single column can be used in a True/False expression that is used as a table filter expression" by using Filter within your CALCULATE.
Here it is as a created column. I used an IF because 'E' code evaluates to a blank and you wanted a 0.
Desired Output Metric = IF(CALCULATE(SUM('Entity Table'[Metric]),FILTER('Entity Table',CONTAINSSTRING('Entity Table'[Descriptor],'Code Table'[Code])))>0,CALCULATE(SUM('Entity Table'[Metric]),FILTER('Entity Table',CONTAINSSTRING('Entity Table'[Descriptor],'Code Table'[Code]))),0)
Here it is as a measure. Be careful to only use this at the Code detail level. When making a measure you need to use aggregate functions to reference your columns, so I am just doing the MIN(Code) since for any single code the Min() will always evaluate to equal that Code. If you try to use this at a higher summary level you may get some odd answers as it will only total for the MIN() code in the data set you are referencing.
Desired Output Metric = IF(CALCULATE(SUM('Entity Table'[Metric]),FILTER('Entity Table',CONTAINSSTRING('Entity Table'[Descriptor],MIN('Code Table'[Code]))))>0,CALCULATE(SUM('Entity Table'[Metric]),FILTER('Entity Table',CONTAINSSTRING('Entity Table'[Descriptor],MIN('Code Table'[Code])))),0)

DAX dynamic RELATED function

I have a data table connected to multiple lookup tables, and I'm trying to find a way to use a the RELATED function to fetch values from a dynamically-selected lookup table based on the values of one of the columns.
e.g.
If the Month column's value is "2018_01", the Type column's value is "Adjustment", and the Variant column's value is "B", look in '2018_01_Adjustment'[Var_B] (essentially '<Month>_<Type>'[Var_<variant>]).
I was hoping DAX had some parallel to Excel's INDIRECT, but from looking through the internet, it appears it doesn't, so I need an alternative.
The most important thing using DAX is your data model.
For this type of question you need to model your data as thus:
First, do you have a date dimension? Once both the fact table (I think that's what you mean when you write "data table") have a link to the dimension table, you can link and group by the dimension you want, for example the MONTH or the YEAR.