Lookup values in DAX from another table without relation (multiple conditions) - powerbi

I have two tables like this.
Table A
Duration
Currency
Type
Rate
1
USD
Annual
0.34
2
EUR
Period
0.41
Table B
Duration
Currency
Type
Product
Rate
1
USD
Annual
Medelli
2
EUR
Period
Longoka
I need to bring the "Rate" in Table A to Table B by the first three column conditions (Duration, Currency, and Type). Which formula do I need to use in the New Column? Thanks beforehand
P.S - I need the unique value of rate, not the sum. And these two tables have no relation

You need a calculated column (not a measure) in Table B:
Rate =
LOOKUPVALUE (
'Table A'[Rate],
'Table A'[Duration], 'Table B'[Duration],
'Table A'[Currency], 'Table B'[Currency],
'Table A'[Type], 'Table B'[Type]
)

Related

How to refer to a column value of a Table in measure

I have a Table in PowerBI, and it has a column which I added by New Column in PowerBI and I have a DAX function to compute the value for each row.
Then I create a new Measure for the same table. I need to create a Measure instead of Column because I need to use SelectedValue.
But for the new measure , I get error saying The value for 'MyColumn' cannot be determined. Either the column does not exist or there is no current row for this column.
Can you please tell me how can I solve this issue>?
I have a slider using the Type of Table 1, so that it has 3 selections "A", "B", "C"
Table 1
Type
A
B
C
And I have another Table 2 in m PowerBI
Table 2
Name
A Count
B Count
C Count
Paul
1
1
Jane
1
2
John
3
I want to build a measure/relationship such that if 'A' of Slider is selected, any row of Table 2 with "A count" has values will be displayed, otherwise, get it hidden.
So in this case,
when 'A' is selected, 'Paul' gets displayed,
when 'B' is selected, 'Jane', 'John' gets displayed,
when 'C' is selected, 'Paul', 'Jane' gets displayed.
Answer to the new question added afterwards:
For DAX calculations you want to avoid “Excel-style” crosstabs like your Table 2 at all costs. The by far easiest way to unpivot this table, would be by using PowerQuery. But if for some reason this is not possible, you can also do it "manually" in DAX:
Stacked 2 =
FILTER(
UNION(
SELECTCOLUMNS(
'Table 2',
"Name", 'Table 2'[Name],
"Attribute", "A Count",
"Value", 'Table 2'[A Count]),
SELECTCOLUMNS(
'Table 2',
"Name", 'Table 2'[Name],
"Attribute", "B Count",
"Value", 'Table 2'[B Count]),
SELECTCOLUMNS(
'Table 2',
"Name", 'Table 2'[Name],
"Attribute", "C Count",
"Value", 'Table 2'[C Count])
),
NOT ISBLANK([Value])
)
The result of this Calculated Table will look like this:
Now you need to extend your "Table 1" by a corresponding Calculated Column:
Attribute = 'Table 1'[Type] & " Count"
All that is left is to create a relationship between the 2 Attribute columns and you're done:
Put 'Table 1'[Type] in a slicer and 'Stacked 2'[Name'] in a table visual and everything will work like a charm:
What you experience is the row context. This only exists in a calculated column, not in a measure. The idea of a measure is to aggregate values in "vertical direction", in a column, whereas a calculated column does calculations in "horizontal direction", row by row.
Bottom line: In measures you always have to use aggregator functions, like SUM(), COUNT(), MIN() around a column reference, e.g.
Measure = SUM('Table'[Column])
But if you still need a row context in a measure, you can use an iterator function like SUMX(), which iterates over a table row by row, thereby establishing the row context, e.g.
Measure =
SUMX(
'Table',
'Table'[Column A] * 'Table'[Column B]
)
which will create a sum product of 'Table'[Column A] and 'Table'[Column B].
Note that if your table happens to have an unique ID column, you can filter your aggregating measure by this column and you'll get values for every row, effectively "removing" the aggregation.

Distinct count of values based on date in DAX

I have a table like shown below:
ID
Date
Asset
Location
145
7/29/22
A
Market
145
7/30/22
A
Warehouse
145
7/29/22
B
Market
145
7/29/22
C
Truck
150
7/30/22
B
Market
145
7/29/22
D
Market
145
7/30/22
A
Market
What I am trying to accomplish is to get a distinct count of IDs for each date with a location filter as well. So I would want a count of ID based on the slicer selected Date of 7/29/22 AND 7/30/22 for the Market Location. The desired result is 2 for the selected dates from the date slicer which directly corresponds to the date column in the table.
I was trying to use this DAX formula and wasn't getting anywhere....
IDsMarket =
CALCULATE (
DISTINCTCOUNT ( 'Products'[ID] ),
ALL ( 'Products' )
)
I have a measure dropped onto a card. I should have specified that. My apologies. I need 1 measure to show me the combined count for the two days selected.
I tried this with countrows as well but of course the result wasn't distinct... Any help would be greatly appreciated!!
The formula you're looking for is
IDsMarket =
CALCULATE(
DISTINCTCOUNT('Products'[ID]),
'Products'[Location] = "Market"
)
The resulting Table will look like this
But if you put the measure on a Card visual, you'll get
So in DAX the same measure can yield 1000 different values - depending on the filter context.
I created a conditional column in Power Query and combined the ID with the "day" number from the date column which allowed me to then do a distinct count on that combined custom column which produced to correct answer. Sorry for all the confusion. One of those days.

Power BI: calculate value using data from related table

I'm trying to calculate the value of item by multiple quantity by unit price from related table.
SALES REPORT
ITEMS
QTY
A
12
B
30
B
45
UNIT PRICE
ITEMS
PRICE
A
$5
B
$9
If you have a relationship set up between the Sales table and the Unit Price table, you can use the RELATED function to retrieve the price.
Add a DAX calculated column to the SALES REPORT table:
Value = Sales[QTY] * RELATED(Products[PRICE])
You can use LOOKUPVALUE to get the result from the other table if you don't have a relationship defined between the tables. As the link says, RELATED is more efficient.
Value = Sales[QTY] * LOOKUPVALUE(Products[PRICE],Products[ITEMS],Sales[ITEMS])
If your schema is like this
Use this measure
Measure =
SUMX ( RELATEDTABLE ( 'SALES REPORT' ), 'SALES REPORT'[QTY] )
* SUM ( 'UNIT PRICE'[PRICE] )

Using RANKX with a YTD Measure does not rank correctly

I am trying to calculate the Cumulative Purchases by YTD. The first step is to rank the items by Cost Amount, but when I try to rank by the [_YTD Cost] measure, the numbers I get do not make sense (skipped numbers, duplicated).
[]
I had 3 slicers: Month, Year and to select Month/YTD measures. Since with the Month calculation I have no problems, I removed the interaction with the Month/YTD slicer and I placed only YTD measures on the table:
Total Purchase Cost = SUM ( Purchases[Amount] )
_YTD Cost = TOTALYTD([Total Purchase Cost], 'dim-calendar'[Date])
_RANK YTD = RANKX(ALLSELECTED(Purchases), [_YTD Cost])
Notes:
I pulled the item from the Item table
The Purchase table is linked to the Date table by Purchase Date
Not 100 % sure on your data model, but try changing your RANKX(ALLSELECTED(***) to reference the Item-column. Like this:
RANKX(ALLSELECTED('Item'[Item]), [_YTD Cost])

Power Bi - Total for Period Year Prior Based On SelectedValue Period

Problem:
Calculate the total for period of prior year of the period(s) selected in slicer. Table has amount, period, and a date, ie 11/1/2020 for period 11/2020, based on the period.
Attempted:
Slicer periods selected are 10/2020 and 11/2020.
Table visual contains the period and amount columns. Data Model Table contains columns, account, amount, period, and period date. Multiple accounts per period will have the same period date, to sort the periods. Example, account 1 through 3 will all have 11/1/2020 as period date for period 11/2020. Calculated Measure created to calculate last year amount is as follows:
Total For Period Last Year =
CALCULATE(
Sum(‘Table’[Amount])
, Filter(
‘Table’
,SAMEPERIODLASTYEAR(‘Table’[Period Date])
)
)
Results:
Calculated measure is added to table but only shows the amount for the period selected in slicer and not the period for the same period but of the prior year.
So 10/2020 amount would be $2000 and the calculation should show amount for 10/2019 with ie $1500.
Period | Amount | Total For Period Last Year
11/2020 $2000 $2000
10/202. $1500 $1500
Desired Result:
Period | Amount | Total For Period Last Year
11/2020 $2000 $1500
10/2020 $1500 $1000
To use time intelligence functions a well formed date table should be used. It's possible to create one using various techniques; an easy one can be found here
DAX 101: Creating a simple date table in DAX
That said, the error in your code is that you iterate over the Table part in the current filter context. You might see a better result (but maybe still not correct, because a Date table is missing, using ALL( 'Table' ) instead of just 'Table'. This way you would FILTER over the whole table instead of just the portion in the current period.
Total For Period Last Year =
CALCULATE(
SUM( 'Table'[Amount] ),
FILTER( ALL( 'Table' ), SAMEPERIODLASTYEAR( 'Table'[Period Date] ) )
)
I'm not sure, since I never use auto date/time, but this could also work by using the Power BI generated Date table when auto date/time is enabled (and if I correctly remember the synstax on how to use it)
Total For Period Last Year =
CALCULATE(
SUM( 'Table'[Amount] ),
FILTER( ALL( 'Table' ), SAMEPERIODLASTYEAR( 'Table'[Period Date].[Date] ) )
)
To use Time intelligence like sameperiodlastyear, you must use date table (Mark as Date Table). Read this description:
https://dax.guide/sameperiodlastyear/#
In order to use any time intelligence calculation, you need a well-formed date table. The Date table must satisfy the following requirements:
All dates need to be present for the years required. The Date table must always start on January 1 and end on December 31, including all the days in this range. If the report only references fiscal years, then the date table must include all the dates from the first to the last day of a fiscal year. For example, if the fiscal year 2008 starts on July 1, 2007, then the Date table must include all the days from July 1, 2007 to June 30, 2008.
There needs to be a column with a DateTime or Date data type containing unique values. This column is usually called Date. Even though the Date column is often used to define relationships with other tables, this is not required. Still, the Date column must contain unique values and should be referenced by the Mark as Date Table feature. In case the column also contains a time part, no time should be used – for example, the time should always be 12:00 am.
The Date table must be marked as a date table in the model, in case the relationship between the Date table and any other table is not based on the Date.
Remarks
The dates argument can be any of the following:
A reference to a date/time column. Only in this case a context transition applies because the column reference is replaced by
CALCULATETABLE ( DISTINCT ( ) )
A table expression that returns a single column of date/time values.
A Boolean expression that defines a single-column table of date/time values.
The result table includes only dates that exist in the dates column.
Internally SAMEPERIODLASTYEAR corresponds to the following call of DATEADD:
DATEADD ( <Dates>, -1, YEAR )