DAX - Lookup value to retrieve exchange rate - dax

In table "Paypal", I have:
And in table "Câmbios":
And now, I'm adding a calculated column to "Paypal" table with the formula:
Câmbio = LOOKUPVALUE('Câmbios'[Câmbio];'Câmbios'[Mês];MONTH('Paypal'[Date]))
Which is returning the error:
A table of multiple values was supplied where a single value was expected.
This doesn't make sense to me.
Can anyone help?
Thanks

The problem is Câmbios table contains repeated values for at least one month and the LOOKUPVALUE function doesn't know which value use to retrieve the specified column.
You can use instead:
Cambio =
CALCULATE (
MAX ( Cambio[Cambio] ),
FILTER ( Cambio, [Mes] = MONTH ( EARLIER ( Paypal[Date] ) ) )
)
Or delete the repeated values from Cambios[Mes].

Related

In DAX, is it possible to check if value exists in another table using measure instead of calculated column?

I'm hoping to create a measure of distinct count of a customer column, on the condition if customers in this column does not exist in another table's customer column.
I know I can create a calculated column checking if the customer exists, and then use the calculate function filtering out those who do exist. But is it possible to achieve this without creating the calculated column?
Please note this is in Power Pivot, not Power BI so I can't really use 'treatas' or 'in'. Thanks a lot.
Assuming tables named Table1 and Table2:
MyMeasure :=
VAR T2Customer =
VALUES( Table2[Customer] )
RETURN
CALCULATE(
DISTINCTCOUNT( Table1[Customer] ),
NOT (
CONTAINSROW(
T2Customer,
Table1[Customer]
)
)
)
Yes, You can achieve it using EXCEPT()function:
Let's say that we have 2 tables like this:
Customer_Table1:
Customer_Table2:
Now we can use this measure to achieve our result:
CountOfDistinctCusts =
COUNTROWS (
EXCEPT (
VALUES ( Customer_Table1[Customer] ),
VALUES ( Customer_Table2[Customer] )
)
)
If we test the code:

What does it mean to use AVERAGEX with the output of VALUES as the table parameter?

I'm trying to understand this tutorial for creating a moving average in DAX. This code snippet is confusing me:
VAR Result =
CALCULATE (
AVERAGEX (
VALUES ( 'Date'[Date] ),
[Sales Amount]
),
Period
)
My understanding is that AVERAGEX takes a table argument and an expression argument, and returns the average values of expression for each row in table. However, in the above example, the table argument is just a set of distinct dates (ie. the result of VALUES('Date'[Date])).
How can I evaluate the [Sales Amount] expression column on a table that only contains dates?

A single value for column .... cannot be determined

I have 2 tables for stock management. 1 for the list of stock and some other properties and 1 for the daily values (i have a relationship between both on the index of the stock).
I would like to have a weekly performance ie the value has increased/decreased by xx from the previous week.
So I created a table (weeklies) with a few rows which correspond to a week for each row. I have 2 columns: 1 is the beginning date of the week, 1 is the last date of the week.
Im creating a calculated third column with the sum of all the values at the beginning date of a given week :
CALCULATE (
SUMX ( Daily_Stock; [Price] * RELATED ( Stock_list[Qty] ) );
FILTER ( Daily_Stock; Daily_Stock[Date] = weeklies[begin_date] )
)
It works fine but I would like to exclude some stocks which were sold before the beginning date (i have other reasons to be able to achieve this) so I'm trying to multiply by 0 if it is the case for that specific stock.
CALCULATE (
SUMX (
Daily_Stock;
[Price] * RELATED ( Stock_list[Qty] )
* IF ( RELATED ( Stock_list[sold_date] ) < weeklies[begin date]; 0; 1 )
);
FILTER ( Daily_Stock; Daily_Stock[Date] = weeklies[begin_date] )
)
There I have the following error :
A single value for column sold_date in table Stock_list cannot be determined.
Tweaking around a little bit and I had the same error on the weeklies table.
Does anyone know what I should be doing here?
I can explain more, I wanted to avoid a too-long post.
thanks
I think the issue is the relation.
I assume the RELATED is based on the stock index you mentioned.
I think related stock_list[sold_date] returns all dates that RELATED stockID has ever been sold.
Which would mean you are trying to compare more than one date with weeklies[begin date].
image copied from powerpivotpro on using VALUES with IF in measures.
If i am right, you need another way of relating to your stocklist to get singular matches. I am not sure if the VALUES solution rob collie uses for measures will work here, but maybe it is worth testing. Rob collie powerpivotpro - Magic of IF(VALUES)

Retrieving a maximum value from a SUMMARIZECOLUMNS table

I have a query and the following results, executed from DAX Studio:
What I would like to do now is to expand the query so that I can retrieve maximum Total Sales from the table that SUMMARIZECOLUMNS produces. For example, based on the rows displayed in the results, I'd like a way to return 10234.35. Is there a way to do this?
Wrap the whole SUMMARIZECOLUMNS part in a MAXX.
MAXX(
SUMMARIZECOLUMNS([...]),
[Total Sales]
)
The MAXX(<table>,<expression>) function iterates through each row of the <table> from its first argument taking the maximum value of the <expression> in the second argument.
As #greggyb points out, a more efficient implementation would be
CALCULATE (
MAXX ( VALUES ( Customers[Customer Key] ), [Sales Amount] ),
FILTER ( Products, Products[Product Name] = "Fabrikam Laptop12v M2080 Silver" ),
FILTER ( 'Calendar', 'Calendar'[Calendary Year] = 2008 )
)
since this doesn't require creating the whole summary table in memory.

DAX code change from calculated column to a measure

I have a fact table with settlement_date, product_id, service_id, location_id, and ticket_id and srv_adjusted_earning columns.
I have determined the DAX query to generate a calculated column that sums the srv_adjusted_earning column over the date range: settlement date and settlement date - 27 days (i.e. a 4 week window) as:
=CALCULATE(
SUM(factService[SRV_ADJUSTED_EARNING]),
DATESBETWEEN
(
factService[SETTLEMENT_DATE],
DATEADD(factService[SETTLEMENT_DATE], -27, DAY),
factService[SETTLEMENT_DATE]
),
FILTER(factService, factService[PRO_ID] = EARLIER(factService[PRO_ID])),
FILTER(factService, factService[SER_ID] = EARLIER(factService[SER_ID])),
FILTER(factService, factService[LOC_ID_SELLING] =
EARLIER(factService[LOC_ID_SELLING])),
FILTER(factService, factService[TIS_ID] = EARLIER(factService[TIS_ID]))
)
I am trying to convert this DAX calculated column to a measure and I tried the following:
blob:=CALCULATE
(
SUM(factService[SRV_ADJUSTED_EARNING]),
DATESBETWEEN
(
factService[SETTLEMENT_DATE],
DATEADD(factService[SETTLEMENT_DATE], -27, DAY),
factService[SETTLEMENT_DATE]
),
ALLEXCEPT(factService, factService[PRO_ID]),
ALLEXCEPT(factService, factService[SER_ID]),
ALLEXCEPT(factService, factService[LOC_ID_SELLING]),
ALLEXCEPT(factService, factService[TIS_ID])
)
But I get:
Error: Calculation error in measure 'factService'[blob]: A single value for column 'SETTLEMENT_DATE' in table 'factService' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result.
Anybody know how I fix this?
As the error mentions, the issue is with factService[SETTLEMENT_DATE]. In the measure, there is no row context so that it knows which date you are talking about, so you need to specify it somehow. I'd suggest using a variable along these lines:
blob :=
VAR SettleDate = MAX ( factService[SETTLEMENT_DATE] )
RETURN
CALCULATE (
SUM ( factService[SRV_ADJUSTED_EARNING] ),
DATESBETWEEN (
factService[SETTLEMENT_DATE],
SettleDate - 27,
SettleDate
),
ALLEXCEPT (
factService,
factService[PRO_ID],
factService[SER_ID],
factService[LOC_ID_SELLING],
factService[TIS_ID]
)
)
Here the variable picks the maximal settlement date in the current filter context. If that's not exactly what you need, adjust the definition accordingly.

Resources