Multiple Selection on Dynamic Financial Quarter by Customer using Power Bi - powerbi

My coworker and I are trying to create a quarterly report that has the ability to slice by customer and reflect their financial year. For example one customers financial year starts in July so Quarter 1 for them should be July 1 - September 30. We are able to get this to work for one specific quarter already but the issue becomes when we introduce the previous quarter. We are wondering if there is a way to get the current quarter based on the customers financial year start month and the previous quarter dynamically. Here is a preview of our report and model:
Report
Model
FY Start Date =
VAR FinancialStartMonth = MIN(Customer_List[FY.Start.Month])
VAR FiscalYearSelected = SELECTEDVALUE('QuarterIDs'[Year])
VAR QuarterNum = SELECTEDVALUE(QuarterIDs[Quarter])
VAR StartDate = SWITCH(QuarterNum, 1, DATE(FiscalYearSelected, FinancialStartMonth, 1), 2,
DATE(FiscalYearSelected, FinancialStartMonth+3, 1), 3, DATE(FiscalYearSelected,
FinancialStartMonth+6, 1), 4, DATE(FiscalYearSelected, FinancialStartMonth+9, 1))
RETURN
StartDate
FY End Date =
VAR FinancialStartMonth = MIN(Customer_List[FY.Start.Month])
VAR FiscalYearSelected = SELECTEDVALUE('QuarterIDs'[Year])
VAR QuarterNum = SELECTEDVALUE(QuarterIDs[Quarter])
VAR StartDate = SWITCH(QuarterNum, 1, DATE(FiscalYearSelected, FinancialStartMonth, 1), 2,
DATE(FiscalYearSelected, FinancialStartMonth+3, 1), 3, DATE(FiscalYearSelected,
FinancialStartMonth+6, 1), 4, DATE(FiscalYearSelected, FinancialStartMonth+9, 1))
VAR EndDate = DATE(YEAR(StartDate), MONTH(StartDate)+3,1) -1
RETURN
EndDate
Total Amount = CALCULATE(SUM(Raw_Data[Amount]),
FILTER('Raw_Data', [Date]))
Amounts by FY =
VAR FinancialStartMonth = MIN(Customer_List[FY.Start.Month])
VAR FiscalYearSelected = SELECTEDVALUE(QuarterIDs[Year])
VAR QuarterNum = SELECTEDVALUE(QuarterIDs[Quarter])
VAR StartDate = SWITCH(QuarterNum, 1, DATE(FiscalYearSelected, FinancialStartMonth, 1), 2,
DATE(FiscalYearSelected, FinancialStartMonth+3, 1), 3, DATE(FiscalYearSelected,
FinancialStartMonth+6, 1), 4, DATE(FiscalYearSelected, FinancialStartMonth+9, 1))
VAR EndDate = DATE(YEAR(StartDate), MONTH(StartDate)+3,1) - 1
RETURN
IF(HASONEVALUE(Dates[Date]),
IF(AND(MIN(Dates[Date]) >= StartDate, MAX(Dates[Date]) < EndDate), [Total Amount], BLANK()),
CALCULATE([Total Amount],
FILTER(ALL(Dates[Date]), Dates[Date] >= StartDate && Dates[Date] < EndDate)))
Big thanks to #EnterpriseDNA for his multiple financial years solution. Thank you for reading this and we appreciate any information or assistance you can provide!

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 Measure - Extrapolate last Date for COUNT

I have a table that contains 3 columns:
Order
Date
State
Each row / record shows if the state was changed:
Now I would like to calculate the number of order that are below state 3 for each date in the calendar.
In the example above you can see there is nor entry for order 100 for 07.01.2022. But for this date the order is still below 3 as you can see in the record before.
How would you do that?
I think this might be better to do in PowerQuery.
Suppose the table mentioned in your question is order_state:
let
Source = Table.FromRows(
{
{100, 1, "1/1/2022"},
{100, 2, "1/5/2022"},
{100, 3, "1/8/2022"},
{101, 1, "1/5/2022"},
{101, 2, "1/6/2022"},
{101, 3, "1/7/2022"},
{102, 1, "1/7/2022"},
{102, 2, "1/9/2022"},
{102, 3, "1/10/2022"}
},
{"Order", "Status", "Date"}
),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}})
in
#"Changed Type"
Then I believe you can calculate what you want with a query like this:
let
start_date = List.Min(order_state[Date]),
end_date = List.Max(order_state[Date]),
days = Duration.Days(end_date - start_date),
dates = Table.FromList(List.Dates(start_date,days,#duration(1, 0, 0, 0)), Splitter.SplitByNothing(), {"Date"}, null, ExtraValues.Error),
joined = Table.AddColumn(
dates,
"order_state_rows",
(current) => Table.RowCount(
Table.Distinct(
Table.SelectColumns(
Table.SelectRows(
order_state,
(row) => (row[Date] <= current[Date] and row[Status] <> 3)
)
,{"Order"}
)
)
)
)
in
joined
Which gives this result:

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

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

Current Fiscal Week with ISO Calendar

I have implemented a fiscal calendar into my Power BI Data Model for some time intelligence magic.
This fiscal calendar has a specific start and end date for each month and is mainly based on ISO 8601 Calendar (Gregorian).
However, my current approach is not working anymore for this year, since the filter "Current Fiscal Week" is not working. If I filter on Current Fiscal Week, then I get the second week of January instead of the first week.
But why?
My code has the following structure:
Calendar =
VAR BaseCalendar =
CALENDAR (
DATE ( 2020, 1, 1 ),
DATE ( 2022, 01, 03 )
)
RETURN
GENERATE (
BaseCalendar,
VAR BaseDate = [Date]
VAR WeekNumber =
WEEKNUM ( BaseDate )
VAR Year =
YEAR ( BaseDate )
VAR FiscalWeek =
WEEKNUM (
BaseDate,
21
)
VAR FiscalYear =
IF (
FiscalWeek < 5
&& WeekNumber > 50,
Year + 1,
IF (
FiscalWeek > 50
&& WeekNumber < 5,
Year - 1,
Year
)
)
RETURN
ROW (
"Fiscal Week", FiscalWeek,
"Current Fiscal Week",
IF (
AND (
FiscalWeek
= WEEKNUM (
TODAY (),
2
),
FiscalYear
= YEAR (
TODAY ()
)
),
TRUE (),
FALSE ()
)
)
)
I think that the Current Fiscal week should be found with the same formulas used to compute the FiscalWeek and FiscalYear. I put the variable definitions for the new variables outside of the GENERATE, since they just depend on TODAY()
VAR TodayBaseDate =
TODAY()
VAR TodayWeekNumber =
WEEKNUM( TodayBaseDate )
VAR TodayYear =
YEAR( TodayBaseDate )
VAR TodayFiscalWeek =
WEEKNUM( TodayBaseDate, 21 )
VAR TodayFiscalYear =
IF(
TodayFiscalWeek < 5
&& TodayWeekNumber > 50,
TodayYear + 1,
IF( TodayFiscalWeek > 50 && TodayWeekNumber < 5, TodayYear - 1, TodayYear )
)
VAR BaseCalendar =
CALENDAR( DATE( 2020, 1, 1 ), DATE( 2022, 01, 03 ) )
RETURN
GENERATE(
BaseCalendar,
VAR BaseDate = [Date]
VAR WeekNumber =
WEEKNUM( BaseDate )
VAR Year =
YEAR( BaseDate )
VAR FiscalWeek =
WEEKNUM( BaseDate, 21 )
VAR FiscalYear =
IF(
FiscalWeek < 5
&& WeekNumber > 50,
Year + 1,
IF( FiscalWeek > 50 && WeekNumber < 5, Year - 1, Year )
)
RETURN
ROW(
"Week day", FORMAT( [Date], "ddd" ),
"Today", TODAY(),
"Fiscal Week", FiscalWeek,
"Current Fiscal Week",
AND( FiscalWeek = TodayFiscalWeek, FiscalYear = TodayFiscalYear )
)
)

iMacros — Setting date to tomrrow and day after tomorrow

I have recorded an iMacro which deals with setting dates in two dropdown lists, tomorrow and day after tomorrow. But people who are using it have to edit the macro everyday to set the dates, including month if it's changing tomorrow or day after tomorrow.
I was wondering if imacro can do it automatically? Setting date 1 to tomorrow's date and date 2 to day-after-tomorrow date. And what about month? What if a new month starting tomorrow or day after tomorrow? Can imacro make some commonsense decisions by itself? :)
This code outputs tomorrow day and day after tomorrow. Need to use Javascript and iMacros command EVAL.
var today = new Date();
var tomorrow = new Date();
tomorrow.setDate(today.getDate() + 1);
var day = tomorrow.getDate().toString();
if (day.length < 2) {day = "0" + day;}
var month = (tomorrow.getMonth() + 1).toString();
if (month.length < 2) {month = "0" + month;}
var year = tomorrow.getFullYear().toString();
var tomorrowDateString = day + "/" + month + "/" + year;
var afterTomorrow = new Date();
afterTomorrow.setDate(today.getDate() + 2);
var day = afterTomorrow.getDate().toString();
if (day.length < 2) {day = "0" + day;}
var month = (afterTomorrow.getMonth() + 1).toString();
if (month.length < 2) {month = "0" + month;}
var year = afterTomorrow.getFullYear().toString();
var afterTomorrowDateString = day + "/" + month + "/" + year;
alert('tomorrow: '+tomorrowDateString+' after tomorrow: '+afterTomorrowDateString);
For iMacros this code will be look like this:
SET tomorrow EVAL("var today = new Date(); var tomorrow = new Date(); tomorrow.setDate(today.getDate() + 1); var day = tomorrow.getDate().toString(); if (day.length < 2) {day = \"0\" + day;} var month = (tomorrow.getMonth() + 1).toString(); if (month.length < 2) {month = \"0\" + month;} var year = tomorrow.getFullYear().toString(); var tomorrowDateString = day + \"/\" + month + \"/\" + year; tomorrowDateString;")
PROMPT {{tomorrow}}
SET afterTomorrow EVAL("var today = new Date(); var afterTomorrow = new Date(); afterTomorrow.setDate(today.getDate() + 2); var day = afterTomorrow.getDate().toString(); if (day.length < 2) {day = \"0\" + day;} var month = (afterTomorrow.getMonth() + 1).toString(); if (month.length < 2) {month = \"0\" + month;} var year = afterTomorrow.getFullYear().toString(); var afterTomorrowDateString = day + \"/\" + month + \"/\" + year; afterTomorrowDateString;")
PROMPT {{afterTomorrow}}