LOOKUPVALUE Returning Rounded Value - powerbi

I am attempting to pull a single value by using LOOKUPVALUE and it is doing so, but instead of returning the value, which is a decimal, it is rounding up to the nearest whole number.
Here's the code that I've tried and and example table of my data set.
Measure =
LOOKUPVALUE(
'Monthly Values'[Requested Value],
[Month Num],
MONTH(TODAY())
)
To be clear, [Month Num] is the Month in numerical form so January = 1, Feb. = 2, etc.
Here is an example table:
Month Irrelevant Month Num Requested Value
1/1/2019 6584651 1 2.48
2/1/2019 6516516 2 2.36
3/1/2019 3464 3 3.32
4/1/2019 6584814 4 2.72
Requested Value is formatted as a "Decimal Number" and limited to 2 decimal places. Also before someone asks, I have double checked the Measure to ensure the measure's formatting is also set to "Decimal Number" with 2 decimal places.
For this case the month is 4 and the formula should spit out "2.72" but instead is spitting out "3".
Is there a way to get around the rounding with LOOKUPVALUE? Alternatively is there another way to lookup this value which would not round the returning value?

I just had the same problem. What I did was to change the data type to decimal in the Power Query, then load the data to Data model (simply refresh the data).
Mine is fixed now.

Use following dax
Measure = FIXED(
LOOKUPVALUE(
'Monthly Values'[Requested Value],[Month Num],MONTH(TODAY())
),
2)

Related

Power BI returning value >0 for when dividing by 0

Trying to calculate YTD Percent off an imported data set, but receiving a value >0 for division where there is no budget or expenditure.
I have tried both of the following dax measures to calculate that column:
Percent = divide(Actuals[Actuals],Budget[Budget])
Percent = IFERROR(Acutals[Acutals]/Budget[Budget], blank())
See photo here:
It appears that these aren't exactly zero just very small (possibly due to floating-point error or something similar). So it isn't actually dividing zeros.
One option to avoid this would be to round your numbers to some number of decimal places before trying to divide. E.g.
Percent = DIVIDE ( ROUND ( Actuals[Actuals], 2 ), ROUND ( Budget[Budget], 2 ) )

DAX Calcuate rolling sum

I have a problem with calculating measure that sums values for 3 previous periods.
Below I attach sample fact table and dict table to show problem I am facing.
date
customer
segment
value
01.01.2021
1
A
10
02.01.2021
1
A
10
03.01.2021
1
A
10
04.01.2021
1
A
10
01.01.2021
2
B
20
02.01.2021
2
B
30
03.01.2021
2
B
40
dict table:
segment
segment_desc
A
Name of A
B
Name of B
Approach I have taken:
last 3 value =
VAR DATES = DATESINPERIOD(facts[date],LASTDATE(facts[date]), -3,MONTH)
RETURN CALCULATE([sum value], DATES)
It produces correct results as long as there is at least one record for April.
When I use filter on segment_desc = 'B'
It produces result as I attached - so we see result in April equals 20, which is obviously not what I wanted. I would expect it to be 50.
Answer to the main question:
time intelligence functions like DATESINPERIOD require a proper calendar table, because they expect continuous dates, without gaps.
Answer to the follow-up question "why the measure shows value for January?"
It's a bit tricky. First, notice that LASTDATE in this filter context returns blank:
So, your DAX measure then becomes this:
last 3 value =
VAR DATES = DATESINPERIOD(facts[date], BLANK(), -3,MONTH)
RETURN CALCULATE([sum value], DATES)
Blank - 3 month does not make sense, so the way DAX resolves this: it replaces BLANK with the first (min) date in the table. In this case, it's 1/1/2021. Then it goes back 3 months from that date. As a result, the final measure is:
last 3 value =
CALCULATE([sum value], {2020-11-01, 2020-12-01, 2021-01-01 })
Since you have no data prior to 2021-01-01, the final result shows only January values.

Convert Numeric type 2000.50 to 2000/09/30 data format in powerbi

I'm trying to convert numeric column which have values as 2000.50/2000.75 and to date format as 2000/09/30 in PowerBI
VAR FiscalYear2 =
LEFT ( Axio_QuarterlySupplyDemandModelNational[Year_Dup], 4 )
RETURN
(
SWITCH (
RIGHT ( Axio_QuarterlySupplyDemandModelNational[Year_Dup], 3),
".00", "3/31/" & FiscalYear2,
".25", "6/30/" & FiscalYear2,
".50", "9/30/" & FiscalYear2,
".75", "12/31/" & FiscalYear2
)
)
I tried the attached code, but some of the values such as 2000.00/2000.50 are not converted as expected.
There are a few approaches that I would think are better than trying to transform the decimal date value in DAX. In the comments, it's been recommended that you do your conversion in Power Query, which is a great suggestion, or use a date table.
We don't always have control over the model tho, so if an additional column on the reporting side is needed, I would suggest that this isn't a formatting question. Fractional years are not a standard date format. You have numbers, so your approach should not be to parse or format, but to calculate.
DateConverted_col =
VAR YearVal = TRUNC(Axio_QuarterlySupplyDemandModelNational[Year_Dup])
VAR QuarterVal = Mod(Axio_QuarterlySupplyDemandModelNational[Year_Dup] * 4, 4) + 1
RETURN Date(YearVal, QuarterVal * 3 + 1, 1) - 1
The TRUNC function drops the decimal portion of the number, leaving you with just the year.
Modulo needs a little massage for fractional numbers. Scaling up by 4 and then finding mod 4 will give you a 0-3 value. Add one and that's the Quarter we're working with.
Multiply the quarter by 3 to get the month number. Figure out that month's last day by adding a whole month to its first day, and then subtracting a single day. Presto, you have the last day of the quarter.
Hope it helps.

Calculate average of data at different levels by year

I have the following Data :
I want to achieve a solution where, when I filter year, it returns me the average of Goodwill of that year and the previous year.
So for my year filter 2017: Average of Goodwill in 2017,2016
year filter 2016: Average of Goodwill in 2016,2015
.... and so on
The year is in General format (NOT Date format) ..
Expected OUTPUT Values:
Not sure what you have tried (or how familiar you are with Power BI/DAX), but you will most likely need to use a combination of DIVIDE, CALCULATE, and ALL. The DIVIDE function isn't super necessary, but it is a great habit to get into as it catches errors. Combining the CALCULATE and ALL will allow you to take the sum for the selected year and the prior year.
What I would do is this:
Goodwill - 2yr Avg =
VAR MaxSelectedYear = MAX([Year])
VAR PriorYear = MaxSelectedYear - 1
VAR MinYearInData = MINX(ALL(Data), [Year])
RETURN
DIVIDE(
CALCULATE(SUM([Goodwill]), FILTER(ALL(Data[Year]), [Year] = MaxSelectedYear))
+
CALCULATE(SUM([Goodwill]), FILTER(ALL(Data[Year]), [Year] = PriorYear))
,
IF(PriorYear < MinYearInData, 1, 2),
BLANK()
)
The IF statement at the end will catch when you are looking at the first year, so that the sum is only divided by 1 instead of 2. The sum will only be 1 year of data since when you filter the Data table to the prior year, there will be no data.
Maybe something like this?
My Average =
IF(HASONEVALUE(Data[Year]);
DIVIDE(
CALCULATE(SUM(Data[Goodwill]);FILTER(ALL(Data[Year]);Data[Year]>=SELECTEDVALUE(Data[Year])-1 && Data[Year]<=SELECTEDVALUE(Data[Year])))
;2);
BLANK()
)
HASONEVALUE, just to identify if my current scope only has 1 year selected for the calculation to be applied.
Than getting the value for the current year and previous year with FILTER and SELECTEDVALUE
I am hardcoding the division by two, but you can adjust it depending on your dataset and purpose.

DAX don't support comparing values of type integer with values type of text

This is DAX formula in Power BI that should create new measure in a table but I faced error saying
DAX don't support comparing values of type integer with values type of text...
This formula is designed to calculated Sum of the Offers for the previous year vs selected one. Offer[Year] is decimal field used in the formula is decimal type:
Offer Amount Prev Year = IF(HASONEVALUE(Offer[Year]), CALCULATE(SUM(Offer[Offer Amount]), Offer[Year] = FORMAT(VALUES(Offer[Year]) - 1, BLANK())))
How to solve the error from above?
The error lies at the FORMAT function.
Offer[Year] is an integer while FORMAT(VALUES(Offer[Year]) - 1, BLANK()) is text, hence DAX don't support comparing the two values.
If you remove the FORMAT function then it should be working.
Offer Amount Prev Year =
IF(
HASONEVALUE(Offer[Year]),
CALCULATE(
SUM(Offer[Offer Amount]),
Offer[Year] = VALUES(Offer[Year]) - 1
)
)