DAX - Plot Previous working day value against today's date - powerbi

I have a table like this,
ReportingDate ReportingDateOrder Status Customer
01/06/2021 1 Active Present
01/06/2021 1 Active
01/06/2021 1 Inactive Present
27/05/2021 2 Inactive Present
27/05/2021 2 Active Present
27/05/2021 2 Active Present
26/05/2021 3 Active Present
I want to generate an visual table like this,
ReportingDate PreviousDaySales
01/06/2021 2
27/05/2021 1
26/05/2021 0
The logic for previousdaysales is the count of active lines for the previous available reporting date.
This is what I have tried so far, but it is returning empty.
PreviousDaySales =
var selectedreportingdate = SELECTEDVALUE('Table1'[Reporting Date])
var selectedreportingrank = CALCULATE(MIN('Table1'[ReportingDateOrder]),FILTER('Table1', 'Table1'[Reporting Date] = selectedreportingdate))
var old_rank = selectedreportingrank + 1
var val1 = CALCULATE(COUNT('Table1'[Action Status]), FILTER('Table1', 'Table1'[Status] = "Active" && 'Table1'[ReportingDateOrder] = old_rank))
return val1
Kindly help me with this.

CurrentActiveLines :=
CALCULATE(COUNTROWS(RStatus),RStatus[Status]="Active")
PreviousReportingDateActiveLines :=
VAR CurrentDate = SELECTEDVALUE( RStatus[ReportingDate] )
VAR PreviousDate =
CALCULATE(
LASTDATE( RStatus[ReportingDate] ),
RStatus[ReportingDate] < CurrentDate
)
VAR Result =
CALCULATE(
COUNTROWS( RStatus ),
RStatus[ReportingDate] = PreviousDate,
RStatus[Status] = "Active"
)
RETURN
Result

Related

How to display total correct in measure

I'm trying to compare difference between years in sales but I'm having the following issue:
I have this:
Valor Actual = -CALCULATE(SUM(Apuntes_Resultado[Total Valor]), Apuntes_Resultado[IDEscenario]=1)
Total Valor Previo =
VAR SubgrupoFilter = ISFILTERED(TCuentas[SubGrupo])
VAR TipoCuentaFilter = ISFILTERED(TTipoCuenta[IDTipoCuenta])
VAR Variablefilter = OR(SubgrupoFilter,TipoCuentaFilter)
VAR Resultado = IF(Variablefilter, [Valor Actual], [ActualAjust])
RETURN
Resultado
I use the above code to have this measure
`
Total Valor Final =
VAR IDVistaDetalle = SELECTEDVALUE(TTipoCuenta[Vista Detalle])
VAR IDDetalle = SELECTEDVALUE(TTipoCuenta[Detalle])
VAR IDDetalleVisible = ISFILTERED(TCuentas[SubGrupo])
VAR Resultado = SWITCH(TRUE(),IDDetalleVisible=TRUE() && IDDetalle = 0, BLANK(),
IDVistaDetalle = 1, [Total Valor Previo],
IDVistaDetalle = 2, [Valor Acumulado])
RETURN Resultado`
this works properly. But i'm trying to normalize this values with laboral day's between years.
To this I have created the following column in my date table:
`Laboral Day =
VAR EsFinDeSemana = Dates[Number Day] IN {7}
VAR EsFestivo =
RELATED(TablaFestivos[Fecha])
RETURN
IF (EsFestivo || EsFinDeSemana,0,1)`
It works fine. Shows properly laboral days and holidays
After that, I'm using the follow measure to calculate the values adjusted by year
`AdjustYear = var total =CALCULATE([Total Valor Final], SAMEPERIODLASTYEAR(Dates[Date]))
var LBCY= CALCULATE(SUMX(Dates,Dates[Laboral Day]))
var PYLB= CALCULATE(SUMX(Dates,Dates[Laboral Day]),SAMEPERIODLASTYEAR(Dates[Date]))
return - DIVIDE(total, PYLB)*LBCY //value from previousyear/PYlaboraldays * CYlaboraldays
`
This is a sample of the result:
as you can see, it is taking the same total for two columns but values are different in rows.....
Totals for 2022 and 2021 are ok, laboral days are okey but B 2021 ajus is taking the same total of A 2021
I'm using my date table to filter by months.
Any help?
myMeasure=
VAR myTbl =
Addcolumns(
Summarize(
Dates
,Dates[yearMonth]
)
,"total",[AdjustYear]
)
RETURN
SUMX(myTbl ,[total])
OR
myMeasure=
VAR myTbl =
Addcolumns(
VALUES(Dates[yearMonth])
,"total",[AdjustYear]
)
RETURN
SUMX(myTbl ,[total])
OR
myMeasure=
VAR myTbl =
Addcolumns(
VALUES(Dates[Month]) -- the same column that you use in the matrix
,"total",[AdjustYear]
)
RETURN
SUMX(myTbl ,[total])

How do I get the period difference for each item in the table in PowerBI desktop?

As seen below, I need to find the difference in quantities for each individual fruits with respect to the previous value. I have previously tried using in built Power BI functions like "PreviousDay()" but haven't found success yet.
You can create these below 2 Measure for your purpose-
Pervious_Quantity =
Var current_row_date = MIN(your_table_name[Date])
var previous_date =
CALCULATE(
MAX(your_table_name[Date]),
FILTER(
ALLEXCEPT(your_table_name, your_table_name[Fruit]),
your_table_name[Date] < current_row_date
)
)
RETURN
CALCULATE(
SUM(your_table_name[Quantity]),
FILTER(
ALLEXCEPT(your_table_name, your_table_name[Fruit]),
your_table_name[Date] = previous_date
)
)
Difference = min(your_table_name[Quantity]) - if([Pervious_Quantity] = BLANK(),0, [Pervious_Quantity])
Here is the final output-

Create column "after" in Powerbi (DAX)

I have the following information and I want to create the column "Later" From isProm : is the next day have the same value or no?
Date isProm Later
2018-06-06 1 1
2018-06-13 1 1
2018-08-20 1 1
2018-09-12 1 0
2018-09-12 0 0
Could you help me to do that with day please?
Thank you very much,
Ana
Create your new Custom Column with this below code-
Option 1:
later =
var current_row_isporm = your_table_name[isProm]
var current_row_date = your_table_name[Date]
var next_date =
CALCULATE(
MIN(your_table_name[Date]),
FILTER(
ALL(your_table_name),
your_table_name[Date] > current_row_date
)
)
var nex_date_isporm =
CALCULATE(
MIN(your_table_name[isProm]),
FILTER(
ALL(your_table_name),
your_table_name[Date] = next_date
)
)
RETURN IF(current_row_isporm = nex_date_isporm,1,0)
Option 2: You can also use this below code for same output-
later =
var current_row_isporm = your_table_name[isProm]
var current_row_date = your_table_name[Date]
var next_date_isporm =
CALCULATE(
MINX(
TOPN(
1,
FILTER(
ALL(your_table_name),
your_table_name[Date] > current_row_date
),
your_table_name[Date].[Date],ASC
),
your_table_name[isProm]
)
)
RETURN IF(current_row_isporm = next_date_isporm,1,0)
Here is the output. I have slightly different output because of date format in my laptop.

Rolling 20 workday revenue

Currently visualizing sales for the past 30days, but looking to switch it in to the past 20 workdays instead, I've got workday column up and running in datetable, so ideally id want to use a filter of workday=1 and grab the 20 newest rows?
Sales 30d =
CALCULATE([Sales],
FILTER(
ALL(d_dates[date]),
d_dates[date]
>TODAY()-30))
This is what im using to show revenue for past 30 days, what'll i need to change?
You can try with this below measure-
slaes_last_20_days =
VAR today = TODAY()
VAR selected_date_min =
MINX(
TOPN(
20,
FILTER(
ALL(d_dates),
d_dates[date].[Date] <= today
&& workday = 1
),
d_dates[date].[Date],
DESC
),
d_dates[date].[Date]
)
RETURN
CALCULATE(
[Sales],
FILTER(
ALL(d_dates),
d_dates[date].[Date] >= selected_date_min
&& workday = 1
)
)
VAR Last20Workdays =
selectcolumns(
TOPN(
20,
FILTER(
d_dates,
d_dates[date] < TODAY()
&& d_dates[workday] = 1
),
d_dates[date],
DESC
),
"WorkDay",
d_dates[date]
)
This worked.

Latest purchase date per category & month - Power BI

Let's assume I have the below dataset:
How can I get the latest purchase date per item and month only for those items that >0?
Expected result:
I've tried a few solutions, one of them, but I cannot figure out all three conditions to be met:
latest_purchase_date = CALCULATE(
MAX(tbl[date]),
FILTER(ALL(tbl),
tbl[quantity]<>0))
Error:
Error:
Did you try the following:
VAR currMonth = MONTH(SELECTEDVALUE(tbl[date]))
VAR currYear = YEAR(SELECTEDVALUE(tbl[date]))
VAR currItem = SELECTEDVALUE(tbl[item])
RETURN
CALCULATE (
MAX(tbl[date]),
FILTER(
ALL(tbl),
tbl[quantity]<>0 &&
tbl[item] = currItem &&
MONTH(tbl[date]) = currMonth &&
YEAR(tbl[date]) = currYear
)
)