Get preview rowvalue filtered by ID - powerbi

I've been struggling with DAX while creating a model to publish on Power BI and the actual problem is presented on the image here. Basically I need a column that shows the value from the predecessor time for the same id.
I did with ranking but wanted to know if it is possible to make it better.
How would you guys do it?
Rank = COUNTROWS(FILTER(test; [id] = EARLIER([id]) && [Date] <= EARLIER([Date])))
Past = if(test[Rank]=1;0; LOOKUPVALUE(test[qt];teste[Rank];test[Rank]-1;test[id];test[id]))

This is a solution tested with the basic model you posted, I don't guarantee this is a machine low cost expression but you can give it a try.
I've created a column called PREVIOUS in which is calculated the previous qty for every row based on the date for the same id.
PREVIOUS =
CALCULATE (
MAX ( TableName[qt] ),
FILTER (
TableName,
EARLIER ( TableName[id] ) = TableName[id]
&& EARLIER ( TableName[date] ) > TableName[date]
)
)
The following is a Power BI table using the PREVIOUS column.
Let me know if this helps.

Related

Calculate ignores outer Filter context

I already posted this question in die power bi community some time ago and revisited my question today. Since I still cannot wrap my head around it I thought I'd ask it here again.
Original: https://community.powerbi.com/t5/Desktop/Calculate-ignores-outer-Filter-context/m-p/1933884#M737076
here is the quesion (again):
After searching "running total" and "filter context" I couldn't find something that matches my question (in a reasonable time).
My base Idea was to compute a running total in DAX. I have a fact table
facts
and a calendar table (marked as such)
calender-table
The "is_this_year" flag marks the current year (2021) with 1, otherwise 0. So the dates start in early 2019.
The setup is as follows: I have a page-level filter on "is_this_year":
page-level-filter
and I wrote the following Measure:
theMeasure =
VAR cur_max = MAX ( 'Calendar'[Date] )
RETURN
CALCULATE ( MIN ( 'Calendar'[Date] ), 'Calendar'[Date] <= cur_max )
Because of the Page-level filter I assument that I would get 1.1.2021 for every row but instead I get this:
result
This lookes like the page-level filter is ignored.
My reasoning here is that in the DAX generated in Power BI I get a filter on Calendar[is_this_year] which is essentially a table
is_this_year
1
In the measure itself I modify the filter context and add a filter on the [Date] Column of the calendar table. My understanding of filter contexts was that, because those filters are applied on different columns they should exist side by side, meaning we have the following filters in the final filter context:
[is_this_year] = 1
[Date] <= 5.5.2021 (for example)
So I would expect to get a row like this:
Date
The measure
05.05.2021
01.01.2021
But I we can see in the picture above I get 03.01.2019 as a measure value.
Can someone help me understand this?
I do see that the Solution posted in the original question works, yet I do not see why I need to pass the filter again.
The solution was:
theMeasure =
VAR cur_max = MAX ( 'Calendar'[Date] )
RETURN
CALCULATE ( MIN ( 'Calendar'[Date] ), 'Calendar'[Date] <= cur_max, VALUES('Calendar'[is_this_year]) )
example file here:
https://1drv.ms/u/s!AtFejN9ixXnChShZpu7MiEgoXVw5?e=ZCdERm
Thank You! I hope someone can shed some light here :)

Power BI - Running total column with many filters

I have table "Sales" (fields: Product, Country, Date, Sales) with monthly sales across many products and countries. Also I have tables with calendar, list of products, list of counties that are linked with this table. I want to add column to "Sales" with running total sales across each Product/Country, see the field with desired result "Running total".
I tried to use
YTD = TOTALYTD(SUM(Sales[Sales]); Calendar[Date]) but it didn't work. I think I need to use filters in TOTALYTD function, but I also didn't manage to understand how. Can you suggest to me a right solution to my case?
Table "Sales"
I was suggested to use this code
Column =
SUMX (
FILTER (
Sales,
Sales[Product] = EARLIER ( Sales[Product] )
&& Sales[Country] = EARLIER ( Sales[Country] )
&& Sales[Date] <= EARLIER ( Sales[Date] )
&& YEAR ( Sales[Date] ) = YEAR ( EARLIER ( Sales[Date] ) )
),
Sales[Sales]
)
It worked.
I partially coped with my issue by creating set of measures for each combination of product and country:
A_US = TOTALYTD(SUM(Sales[Sales]);'Calendar'[Date];FILTER(All(Sales);Sales[Product]="A"&&Sales[Country]="US"))
A_Canada = TOTALYTD(SUM(Sales[Sales]);'Calendar'[Date];FILTER(All(Sales);Sales[Product]="A"&&Sales[Country]="Canada"))
and so on. But what if i have 100 products and 30 countries? I think I need to create a column "Running total" in "Sales" that calculates running total for each product and aech country.
The problem of the TOTALYTD function is that it takes only one filter.
The tricks is to use the filter function like you do on the second response.
To use only one column for all product and country you have to get the context of the current row .
To achieve this you have the function earlier in dax.
Here the documentation about earlier : https://learn.microsoft.com/en-us/dax/earlier-function-dax
The column need to be build with this expression :
TOTALYTD(SUM(Sales[Sales]),'Calendar'[Date],filter(Sales,and(Sales[Country]=EARLIER(sales[Country]),sales[Product] = EARLIER(sales[Product]))))

Filter existing table to another table without adding measures or column on existing table

I want to create a table based on input table.
Input table is:
The new table filters the input table to show the last entry of every day.
I have tried working with measure but sometimes cant tell if it is working right until I graph it in pivot tables which is not so bad but sometimes just doesn't show me what I need to see exactly.
I have tried this measure:
History_Daily Efficiency =
VAR LastDailyEfficiency =
GENERATE(
VALUES ('Table_Full'[Cell]),
CALCULATETABLE (
TOPN (
1,
GROUPBY (
'Table_Full',
'Table_Full'[Date],
'Table_Full'[Time],
'Table_Full'[Efficiency]
),
'Table_Full'[Date], DESC,
'Table_Full'[Time], DESC,
'Table_Full'[Efficiency], ASC
)
)
)
RETURN
CALCULATE (
AVERAGE('Table_Full'[Efficiency]),
TREATAS( LastDailyEfficiency, 'Table_Full'[Cell], 'Table_Full'[Date], 'Table_Full'[Time], 'Table_Full'[Efficiency]),
'Table_Full'[Efficiency] < 80
)
But I got this:
I would like to see this as the output:
You can create a new table:
LastDayCount = GROUPBY(Table_Full;Table_Full[lob/Part Number];Table_Full[Date];"LastDate";MAXX(CURRENTGROUP(); Table_Full[DateTime]))
This will create a table with the last DateTime of the day.
Next we add a column giving us the max of that particular last datetime of the day. I noticed that you have more the same entries, the logic below takes the max part count at the end of the day when more than one entry.
Count =
CALCULATE(MAX(Table_Full[Part Count]);
FILTER(Table_Full;LastDayCount[Table_Full_lob/Part Number] = Table_Full[lob/Part Number]
&& LastDayCount[LastDate] = Table_Full[DateTime]))
End result:

How to Show last period (Fiscal Year) of sale, based on multi-filters report

I tried create a dashboard based on fiscal year, with more Filters, like region, sales rep name, ...
Example files avaliable on dropbox:
https://www.dropbox.com/sh/l25kdz6enmg35yb/AABPuOk3kKOpfQdKDfRUcnX2a?dl=0
On my closest attempt, i tried this follow:
Add one column on my data set, naming each period as distinct number, like: "17";"18";"19", due to deslocated fiscal year (april to march).
Then create a measure:
PREVIOUS CROP_YEAR = SWITCH(TRUE();
SELECTEDVALUE('dataset'[Crop-X])=16;(CALCULATE(SUM('dataset'[Order Value]);ALL('dataset')));
SELECTEDVALUE('dataset'[Crop-X])=17;(CALCULATE(SUM('dataset'[Order Value]);ALL('dataset')));
SELECTEDVALUE('dataset'[Crop-X])=18;(CALCULATE(SUM('dataset'[Order Value]);ALL('dataset')));
SELECTEDVALUE('dataset'[Crop-X])=19;(CALCULATE(SUM('dataset'[Order Value]);ALL('dataset')));
0)
Expected output was:
Values based on all filters applied, But instead i just get an empty charts
The measure is return the total because you are explicitly asking for it by using the ALL function. This removes all the filters from the dataset thus returning a grand total. This can work but it creates a complexity in your dataset with respect of having two time dimensions. The way to solve this is to first make sure you filter the date correctly with respect to both dimensions
PREVIOUS YEAR =
CALCULATE(
SUM('dataset'[Order Value]);
FILTER(
ALL ( 'dataset' ) ;
AND (
'dataset'[Crop-X] = MAX('dataset'[Crop-X]) -1 ;
'dataset'[YEAR] = MAX('dataset'[YEAR] ) -1
)
)
)
Furthermore, this measure still uses the ALL function which means any other filters get ignored. Using ALLSELECTED instead would result in the relative time filtering to result in nothing as soon as you select any time based slicer in your dashboard, this prevents the filter from looking at any other part of the dataset that is not within the primary sliced dataset. The workaround would be to use ALLEXCEPT and add the filters you want to be able to use as arguments. Downside is that any filter you add to your dashboard will have to be added to the exception manually.
PREVIOUS YEAR =
CALCULATE(
SUM('dataset'[Order Value]);
FILTER(
ALLEXCEPT( 'dataset' ; Dim1[Group] ; Dim1[Manager] ; Dim1[Region] ) ;
AND (
'dataset'[Crop-X] = MAX('dataset'[Crop-X]) -1 ;
'dataset'[YEAR] = MAX('dataset'[YEAR] ) -1
)
)
)

Power bi - User retention rate calculation

I'm fairly new to Power bi and have tried and searched for this almost all the forums but couldn't find anything similar to mine.
So.. I have a table like the following (Something similar)
I would like to calculate the retention rate of the users (who actually came back).
What I have done so far:
RetentionRate = (ReturningUsers / PreviousDayDistinctUsers)*100%
ReturningUsers = DistinctUsers - NewUsers
PreviousDayDistinctUsers = CALCULATE(DISTINCTCOUNT(table[User], PREVIOUSDAY(table[Date])
NewUsers = CALCULATE(DISTINCTCOUNT(table[User] ), table[MonthlyNewUsers] = BLANK () )
The above looks to be working, but the only drawback was with the PreviousDayDistinctUsers as it is only considering the previous day (Not all the days from the starting to that day).
So how do I write a measure to calculate the DistinctUsers for all the days until today?
PreviousDayDistinctUsers =
VAR Current_Day = LASTDATE ( table[Date] )
RETURN
CALCULATE ( DISTINCTCOUNT ( table[User] ), table[Date] < Current_Day )
How it works:
First, save last date in a filter context into a variable (instead of LASTDATE, you can also use MAX function).
Second, filter table User by all dates that are less than the saved date, and count distinct users in the filtered table.