Getting equivalent min percentage year - powerbi

I have the data bellow that shows year, value and cumulative percentage
create table #data (cost money, Current_Year int, Cumulative money, [Percentage] decimal(9,4))
insert into #data values
(43000000,2014,43000000,0.000647922726779209),
(54000000,2015,97000000,0.00146159312785077),
(60777777.75,2016,157777777.75,0.00237739088337079),
(123777777.8,2017,281555555.55,0.0042424707742277),
(126555555.5,2018,408111111.05,0.00614940613721881),
(60761257937,2019,61169369048.05,0.921698241604101),
(492914890.8,2020,61662283938.85,0.929125468583571),
(456692522.2,2021,62118976461.05,0.936006897985512),
(416673457.6,2022,62535649918.65,0.942285321307017),
(422831144,2023,62958481062.65,0.94865652846487),
(371598857.2,2024,63330079919.85,0.954255769042197),
(346364399,2025,63676444318.85,0.959474777550553),
(336826238.7,2026,64013270557.55,0.964550065341932),
(350483170.1,2027,64363753727.65,0.969831135371267),
(334551349.4,2028,64698305077.05,0.974872145198032),
(310619368.1,2029,65008924445.15,0.97955254863919),
(344523398.6,2030,65353447843.75,0.984743817008031),
(345189362.5,2031,65698637206.25,0.989945120101198),
(332918789.2,2032,66031555995.45,0.994961530559209),
(334382754.3,2033,66365938749.75,1)
select * from #data
drop table #data
I want to be able to select a percentage in my case between 94% and 95% in my filter to return the equivalent min year for that that percentage and the percentage
I tried
95%Range = var result =IF([Cost constant %]>=0.94 && [Cost constant %]<=0.95,[Cost constant %],BLANK())
return
IF(result <>BLANK(),MAX(Calendar_Date[Current_Year])
it is not working
expected output
)

I was able to do these using two measures:
Min Year = min(Query1[Current_Year])
Percentage Lookup = LOOKUPVALUE(Query1[Percentage],Query1[Current_Year],[Min Year])
You can then add percentage to a filter or slicer and Power BI will return the earliest year that matches the filter criteria. Here are the results in a simple table visual:

Related

PowerBi DAX measure to sum duration of timespans filtered by current slicer

I need a DAX measure that gives me the sum of durations for multiple categories restricted by a date slicer.
In this simplified example there are 2 categories with 3 subcategories each. A DateTime Slicer on the dashboard is set to the timespan of 2nd of January 2021 noon to 6th of January midnight. I need the summed up duration of all categories in this timespan.
Input data:
A table containing multiple rows for each category with a start date and an end date.
The complicated part is that there are pauses between the timestamps.
Desired output:
A table on the dashboard containing the category and a calculated measure for the summed up duration during the sliced timespan.
When changing the slicer the meaure shall change as well.
My current solution for this problem is an M formulato create a list of all days in each timespan and to unpivot all lists. In the dashboard the count of rows gives you the number of days in the selected timespan. This solution though reqires a much larger input table and soes not work if you want to be exact on the second, only on days.
I tried so solve this via a measure but didn't make any progress worth showing here.
all datetime values are in the format dd.mm.yyyy hh:mm:ss (24h system)
I found a way to do it by using 2 measures.
First measure calculates the time during the timespan for each element:
I use one Date Table only consisting of all dates available which is the input for the slicer and the data Table called "Data".
duration_in_timespan_single =
VAR MinTs = MIN ('Date'[Date])
VAR MaxTs = MAX ('Date'[Date])
VAR MinUtcMin = MIN ('Data'[Date_Start])
VAR MaxUtcMax = MAX ('Data'[Date_End])
RETURN
IF(
AND(MinUtcMin >= MinTs, MinUtcMin <= MaxTs),
IF(
MaxUtcMax <= MaxTs,
CONVERT((MaxUtcMax-MinUtcMin),DOUBLE),
CONVERT((MaxTs-MinUtcMin),DOUBLE)),
IF(
MinUtcMin < MinTs,
IF(
MaxUtcMax > MinTs,
IF(
MaxUtcMax <= MaxTs,
CONVERT((MaxUtcMax-MinTs),DOUBLE),
CONVERT((MaxTs-MinTs),DOUBLE)
),
0
),
0
)
)
The second measure just sums up the first for each category:
duration_in_timespan = SUMX('Data',[duration_in_timespan_single])

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])

Fetch specific record value from a table in PowerBI

I'm trying to dynamically calculate the % Change of the prices in a table, where each price will be compared with the first price (Price in the first record, ordered by date). There is a date filter, so the first record being displayed will change accordingly. Date column values are unique.
Assume the user applies a filter for date BETWEEN '15-APR-2021' AND '30-APR-2021'. Then the expected '% Change' column would look like:
For this example, I had to hardcode the starting price in the calculation:
% Change = (('table1'[price]/3218.95) -1)*100
I tried the below, but it doesn't return a static value of 3218.95. Instead, it re-calculates it at record level rather filtered table level as we see in the above screenshot :
first_date = (MIN(table1[Date]))
first_price = LOOKUPVALUE('table1'[price],table1[Date],'table1'[first_date])
% Change = (('table1'[price]/first_price) -1)*100
I'm new to PowerBI DAX. Logically the SQL would look like so:
SELECT
date,
price,
((
price /
( -- Gets the first price
SELECT price FROM table1
WHERE date IN (SELECT MIN(date) FROM table1 WHERE date BETWEEN '15-APR-2021' AND '30-APR-2021')
)
)-1) * 100 as '% change'
FROM table1
WHERE date BETWEEN '15-APR-2021' AND '30-APR-2021'
IF you want to get the first price you can use the following DAX:
first_price = CALCULATE(MIN('table1'[price]), FILTER( 'table1', MIN(table1[Date])))
As for the % Change:
% Change =
var curPrice = 'table1'[price]
var first_price = CALCULATE(MIN('table1'[price]), FILTER( 'table1', MIN(table1[Date])))
return ((curPrice/first_price) - 1) * 100

Calculate date diff between column date for each row and min slicer date

I think that the following formula summarize pretty well what I want to achieve:
date diff =
ABS (
DATEDIFF (
data_table[login_date],
SELECTEDVALUE ( 'Date'[Date] ),
DAY
)
)
but it returns me the following error
A single value for column 'login_date' in table 'data_table' 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.
In other word I want have a column in my data_table with date diff calculated dynamically based on min slicer date selection.
My final goal is to filter out dynamically users that has not been logged for the last 3 months based on the slicer date range.
Here is the dataset
user_id, login_date
111, 01/02/2021
222, 02/15/2021
444, 03/15/2021
555, 01/15/2021
I want user ID to be filtered out when the number of days between the max date of the date range and the day of the last connection is higher than 90 days.
Edit
I'm adding a different formula I'm working on but having few issues to make it work
active users = CALCULATE( DISTINCTCOUNT(data_table[id]), ( FILTER ( VALUES ( data_table[id] ), DATEDIFF(IF( ISBLANK(SELECTEDVALUE(data_table[login_date])),[Min range date],SELECTEDVALUE(data_table[login_date])),[Max range date],DAY) < 90 ) ))
You can't have a dynamically calculated column, but you can use a measure to do this. The issue that you have with your calculation is that it needed to do a row by row evaluation, rather than over a column. That is why you get an 'A single value for column 'login_date' in table 'data_table' cannot be determined' error.
In this case you can use SUMX, as this is a iterator and it will do row by row. So using the following measures:
Selected Date = SELECTEDVALUE('Calendar'[Date])
This reads the date selected. You can wrap it with a MIN/MAX if needed depending on how your slicer is set up. You can change the slicer to single select, it you just want one value.
Date Calc = SUMX('Table', DATEDIFF('Table'[Login_date], [Selected Date], DAY))
This uses SUMX to calculate on a row by row level.
You can then use this measure to drive your visual. In this example I've filtered out those over 30 days since the login
If you choose a new date, it will recalculate straight away. This should set you on the right path for your use case.

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.