How to calculate Dynamic Moving Average without any date reference? - powerbi

I am looking for some clarification with regards to moving average calculation. My data looks like the screenshot attached.
I checked online for suggestion but it went awry. Most of the moving averages work with date (in date format). As I am having Day...only in numeric format...I am not sure which function would help me to get Mov_Avg for 3 Day time frame on the Increment col. My desired output should be col 3_day_Avg

You can use your column Increase (created using Power Query) and create this below measure for your required output-
last_three_day_average =
VAR current_row_day = MIN(your_source_table_name[day])
VAR current_row_country = MIN(your_source_table_name[country])
VAR current_row_case_death = MIN(your_source_table_name[cases/death])
VAR current_row_count = MIN(your_source_table_name[count])
RETURN
CALCULATE(
AVERAGE(your_source_table_name[increase]),
FILTER(
ALL(your_source_table_name),
your_source_table_name[country] = current_row_country
&& your_source_table_name[cases/death] = current_row_case_death
&& your_source_table_name[day] <= current_row_day
&& your_source_table_name[day] >= current_row_day - 2
)
)
Here is the final output-
Alternatively, if you wants not to use Power Query, You can use Custom column and that case you have to convert your Measure increase_using_measure as a column. As aggregation is required, you can not refer a measure for that. Here below is the code for column-
increase_using_column =
VAR current_row_day = your_source_table_name[day]
VAR current_row_country = your_source_table_name[country]
VAR current_row_case_death = your_source_table_name[cases/death]
VAR current_row_count = your_source_table_name[count]
VAR previous_day_count =
LOOKUPVALUE(
your_source_table_name[count],
your_source_table_name[day],current_row_day-1,
your_source_table_name[country],current_row_country,
your_source_table_name[cases/death],current_row_case_death
)
RETURN if(previous_day_count = BLANK(),current_row_count, current_row_count - previous_day_count)
Now you can use the above column created using DAX in your Last 3 days average calculation as below-
last_three_day_average =
VAR current_row_day = MIN(your_source_table_name[day])
VAR current_row_country = MIN(your_source_table_name[country])
VAR current_row_case_death = MIN(your_source_table_name[cases/death])
VAR current_row_count = MIN(your_source_table_name[count])
RETURN
CALCULATE(
AVERAGE(your_source_table_name[increase_using_column]),
FILTER(
ALL(your_source_table_name),
your_source_table_name[country] = current_row_country
&& your_source_table_name[cases/death] = current_row_case_death
&& your_source_table_name[day] <= current_row_day
&& your_source_table_name[day] >= current_row_day - 2
)
)
Output will be same as shown above.

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

DAX Power BI - addcolumns to calculated table

I have a task to compare 2 dynamic periods from the table (MOO).
Th idea is to get clients which are in both dates and compare them by 1 field (Rating_rank).
But my created field calculates max from all table, not grouping by client.
What should id do?
rate_worse_tab =
var min_dt = calculate(min('MOO'[value_day]),ALLSELECTED('MOO'[value_day]))
var max_dt = calculate(max('MOO'[value_day]),ALLSELECTED('moo'[value_day]))
var cur_cl = CALCULATETABLE(values(MOO[CLIENT_UK]),filter(MOO,MOO[VALUE_DAY]=max_dt))
var old_cl = CALCULATETABLE(values(MOO[CLIENT_UK]),filter(MOO,MOO[VALUE_DAY]=min_dt))
var combo_table = CALCULATETABLE(values(MOO[CLIENT_UK]),filter(MOO, MOO[CLIENT_UK] in cur_cl && MOO[CLIENT_UK] in old_cl))
var f_table = ADDCOLUMNS(combo_table,"Old_rate_rank",calculate(max(MOO[Rating_rank]),filter(MOO,MOO[VALUE_DAY]=min_dt && MOO[CLIENT_UK] in combo_table)))
return
f_table

Trying to get list of Venues with respect to Suppliers

Hi guys so i am trying to build a report where i have to show new customers this month compared to last month. I am able to calculate new customers fine but i have to show them in a Matrix in Power Bi. This is the Code i am using
New Customers This Month =
VAR ThisMonth =
SELECTEDVALUE( DailyReport[DateCreated].[MonthNo])
VAR ThisYEAR =
SELECTEDVALUE(DailyReport[DateCreated].[Year])
VAR SelectedSupplier =
SELECTEDVALUE(DailyReport[SupplierName])
VAR LastMonth = ThisMonth - 1
VAR CustomersThisMonth =
DISTINCT(
SELECTCOLUMNS(
FILTER( ALL( DailyReport ), DailyReport[DateCreated].[MonthNo] = ThisMonth && DailyReport[DateCreated].[Year] = ThisYEAR && DailyReport[SupplierName] = SelectedSupplier),
"C1", DailyReport[VenueName]
)
)
VAR CustomersLastMonth =
DISTINCT(
SELECTCOLUMNS(
FILTER( ALL( DailyReport ), DailyReport[DateCreated].[MonthNo] = LastMonth && DailyReport[DateCreated].[Year] = ThisYEAR && DailyReport[SupplierName] = SelectedSupplier),
"C1", DailyReport[VenueName]
)
)
VAR T1 =
EXCEPT(CustomersThisMonth, CustomersLastMonth )
RETURN
CONCATENATEX( T1, [C1], ", " )
I am getting result like this, two columns one for supplier and one for new venues this month
getting result like this
But expected result should be like this, you know like a list
expected result
i think it may be causing due to how i am concatening it in the last line in code
and please let me know if you need any more information

grand total empty in table

I've the following pbix https://1drv.ms/u/s!Amd7BXzYs7AVlXPjuP0CfblYyc5K?e=kNnEiN
I've the following measure 
Measure2 = var _1=max(MaxPropre[Nb]) +0
var _min = minx(ALLSELECTED('Date Filter'), 'date Filter'[Date]) var ax = maxx(ALLSELECTED('MaxPropre'), 'MaxPropre'[Date])
var _max = maxx(ALLSELECTED('date Filter'), 'date Filter'[Date]) VAR dt =SELECTEDVALUE ( 'DimDate'[date] )
return
CALCULATE(if((max('DimDate'[Date]) <_min || max('DimDate'[Date]) >_max ) , BLANK(), if(ax >=dt,[const],0)))
 
why I'm not getting the grand total please?
You can add another measure like:
Measure3 = IF(HASONEFILTER('DimDate'[date]),[Measure2],SUMX(VALUES('DimDate'[date]),[Measure2]))
...and use it in your table.
This site explains what you're dealing with fairly well.

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