I am new in powerBI, I loonking to display data in during a closing period and in my case it is from :
31/12/Y-1 to 31/12/Y my issue it is with slicer the year filter all date in the current year and not taking into account the value in 31/12/Y-1
How can I set PowerBI to do so,
thanks for your support
Display Sum of data in a defined period 31/12/Y-1 till 31/12/Y, user can select the period that they want to display and the data will update via PowerBI
Slicer below :
Use this calculated column
Closing Period =
VAR thisYear = YEAR(TODAY())
VAR startDate = DATE(thisYear -1 , 12, 31)
VAR endDate = DATE(thisYear, 12, 31)
RETURN
IF(startDate <= 'Date'[Date] && 'Date'[Date] <= endDate, thisYear)
and filter the 'Date'[Closing Period] column on the current year.
You can use a new calculated column for the slicer in your case:
Filter_date = DATEADD ( 'Date Table'[date].[Date], +1, DAY )
Related
We're currently rebuilding basic emailed reports built using T-SQL to be paginated reports published on Power BI.
We're muddling through by creating the tables we need with the appropriate filters in Power BI Desktop to reconcile the numbers, then taking the DAX code from them using the Performance Analyser.
The one I'm working at the minute has a simple bit of SQL code to get data for a previous calendar month. I have no idea how or if it's possible for this to exist in DAX?
-- Validation to get previous month
IF (MONTH(GETDATE()) - 1) > 0
SET #MONTH = MONTH(GETDATE()) - 1
ELSE
SET #MONTH = '12'
-- Validation to get year of previous month
IF (#MONTH < 12)
SET #YEAR = YEAR(GETDATE())
ELSE
SET #YEAR = YEAR(GETDATE()) - 1
-- Set start date and finish date for extract
SET #PERIOD = #YEAR + RIGHT('00' + #MONTH, 2)
It needs to become a hidden SSRS parameter or just inline code to be used with this DAX variable:
VAR __DS0FilterTable =
TREATAS({"202212"}, 'Org View_VaultexCalendar'[Calendar Month No])
So the "202212" would become #period or the equivalent if doable without a parameter.
SSRS Parameter:
=IIF(
Month(Today()) > 1,
Year(Today()) & RIGHT("00" & Month(Today()) - 1, 2),
Year(Today())-1 & "12"
)
DAX expression:
IF (
MONTH ( TODAY () ) > 1,
YEAR ( TODAY () ) & FORMAT ( MONTH ( TODAY () ) - 1, "00" ),
YEAR ( TODAY () ) - 1 & "12"
)
In both cases we look at today's month and check if it's after January. If it is, take the current year and concatenate it with the current month less one and padded with a leading zero when needed. In the other case we know that the month is January so take the current year less one and concatenate it with "12"
I have a dimdate table that is represented below. I have each day flagged as BusinessDay Y/N. I also have a DimSalesRep table that has a daily goal for each rep. I want to be able to allow users to input a StartDt and EndDt with filters on the report and have a calculated column look at the business days between those dates. I can calculate daysbetween with defined dates but I am unsure how I would use DAX with variable dates that are applied through Report filters.
I should also note I am not sure how best to handle a startdt and enddt filter based of the column, TheDate
Cheers!
Reference your dimdate table twice
StartDate = 'dimdate'
EndDate = 'dimdate'
and use this measure:
Num BusinessDays =
CALCULATE(
COUNTROWS('dimdate'),
'dimdate'[BusinessDay] = "Y",
'dimdate'[Date] >= SELECTEDVALUE(StartDate[Date]),
'dimdate'[Date] <= SELECTEDVALUE(EndDate[Date])
)
I have some users with different game_id.
for each user, I want to find the second minimum date. (column: min2_date)
If a user doesn't have a second date (look at user_id: 2, in this example), his min2_date should be -1.
If the second minimum date is the same as the first minimum date(look at user_id: 4), we should write that date in the min2_date column.
I don't know How I should calculate the second minimum date in Power BI.
please help me if you know.
Because you dont want a real second min date (but date in second row by order), we must try some trickyway.
One of way that we can do that is:
MinDate2 =
var _countrows = CALCULATE(countrows(VALUES(games[dates])), ALL(games[dates]) )
return
if(_countrows = 1, -1,
FORMAT(DISTINCT(TOPN(1,TOPN(2,CALCULATETABLE(SELECTCOLUMNS(games, "dates",games[dates]), ALL(games[dates])),[dates], asc), [dates], desc)), "yyyy-mm-dd")
)
where MinDate and MinDate2 are measures.
AS a calculatedColumn:
MinDate2_col =
var _countrows = CALCULATE(countrows(VALUES(games[dates])), ALL(games[dates]) )
return
if(_countrows = 1, "-1",
FORMAT(DISTINCT(TOPN(1,TOPN(2,CALCULATETABLE(SELECTCOLUMNS(games, "dates",games[dates]), ALL(games[dates])),[dates], asc), [dates], desc)), "yyyy-mm-dd")
)
I tried generating a DAX measure as a rolling average. I don't quite know how to insert the measure I need rolling averaged as "CPI_annualized". It is not giving an option to bring CPI_annualized into the Rolling Average calc.
This is the error I am given when trying to construct the RollingAverage VAR in the measure P_MA
The syntax for 'CALCULATE' is incorrect. DAX(VAR __LAST_DATE = LASTDATE('public econometrics'[date]) ......
This is the DAX measure that I am trying to complete :
P_MA =
VAR __NUM_PERIODS = 3
VAR __LAST_DATE = LASTDATE('public econometrics'[date])
VAR RollingAverage =
AVERAGEX(
DATESBETWEEN(
'public econometrics'[date],
DATEADD(__LAST_DATE, -__NUM_PERIODS, MONTH),
__LAST_DATE)
CALCULATE([CPILFESL]))
)
RETURN RollingAverage
This is the DAX measure that I am trying to use in the rolling calculation with the data in my dB given monthly. This works as below.
CPI_annualized = (CALCULATE(SUM('public econometrics'[value]),'public
econometrics'[econometric_name]=="CPILFESL")/CALCULATE(SUM('public
econometrics'[value]),'public
econometrics'[econometric_name]=="CPILFESL",SAMEPERIODLASTYEAR('public econometrics'[date])))-1
Inserting this measure in a line chart gives my this table.
date
CPILFESL
1 January, 1978
6.41%
1 February, 1978
6.20%
I think you are getting error message due to this line:
CALCULATE([CPILFESL])
In Dax Calculate measure, you cannot directly calculate a value, instead you have to include a number calculation function such as sum, max, min, here is the example:
CALCULATE(sum([CPILFESL]))
In addition, you can get rid of Calculate measure when there is no filter expression, such as sum([CPILFESL]) is enough.
A proper way of using calculate is to filter the value, as shown in online documentation
CALCULATE(
SUM(Sales[Sales Amount]),
'Product'[Color] = "Blue"
)
You can find many usefull templated here:
https://www.daxpatterns.com/month-related-calculations/
Moving average 3 months
VAR MonthsInRange = 3
VAR LastMonthRange =
MAX ( 'Date'[Year Month Number] )
VAR FirstMonthRange =
LastMonthRange - MonthsInRange + 1
VAR Period3M =
FILTER (
ALL ( 'Date'[Year Month Number] ),
'Date'[Year Month Number] >= FirstMonthRange
&& 'Date'[Year Month Number] <= LastMonthRange
)
VAR Result =
IF (
COUNTROWS ( Period3M ) >= MonthsInRange,
CALCULATE (
AVERAGEX ( Period3M, [Sales Amount] ),
REMOVEFILTERS ( 'Date' )
)
)
RETURN
Result
i need to calculate difference between two date excluding sunday. I have table with dates and i need to calculate number of dates of repeated days from last date.
if i have dates like that
27-05-2017
29-05-2017
30-05-2017
I use this code in script
date(max(Date)) as dateMax,
date(min(Date)) as dateMin
And i get min date = 27-05-2017 and max date = 30-05-2017 then i use in expressions
=floor(((dateMax - dateMin)+1)/7)*6 + mod((dateMax - dateMin)+1,7)
+ if(Weekday(dateMin) + mod((dateMax - dateMin)+1,7) < 7, 0, -1)
And get result 3 days. Thats OK, but the problem is if I have next dates:
10-05-2017
11-05-2017
27-05-2017
29-05-2017
30-05-2017
When use previously code I get min date = 10-05-2017 and max date = 30-05-2017 and result 18, but this is not OK.
I need to count only dates from
27-05-2017
29-05-2017
30-05-2017
I need to get max date and go throw loop repeated dates and if have brake to see is that date sunday if yes then step that date and continue to count repeated dates and if i again have break and if not sunday than close loop and remember number of days.
In my case instead of 18 days i need to get 3 days.
Any idea?
I'd recommend you creating a master calendar in the script where you can apply weights or any other rule to your days. Then in your table or app you can just loop through the dates or perform operations and sum their weights (0: if sunday, 1: if not). Let's see an example:
// In this case I'll do a master calendar of the present year
LET vMinDate = Num(MakeDate(year(today()),1,1));
LET vMaxDate = Num(MakeDate(year(today()),12,31));
Calendar_tmp:
LOAD
$(vMinDate) + Iterno() - 1 as Num,
Date($(vMinDate) + Iterno() - 1) as Date_tmp
AUTOGENERATE 1 WHILE $(vMinDate) + Iterno() - 1 <= $(vMaxDate);
Master_Calendar:
LOAD
Date_tmp AS Date,
Week(Date_tmp) as Week,
Year(Date_tmp) as Year,
Capitalize(Month(Date_tmp)) as Month,
Day(Date_tmp) as Day,
WeekDay(Date_tmp) as WeekDay,
if(WeekDay = '7',0,1) as DayWeight //HERE IS WHERE YOU COULD DEFINE A VARIABLE TO DIRECTLY COUNT THE DAY IF IT IS NOT SUNDAY
'T' & ceil(num(Month(Date_tmp))/3) as Quarter,
'T' & ceil(num(Month(Date_tmp))/3) & '-' & right(year(Date_tmp),2) as QuarterYear,
date(monthStart(Date_tmp),'MMMM-YYYY') as MonthYear,
date(monthstart(Date_tmp),'MMM-YY') as MonthYear2
RESIDENT Calendar_tmp
ORDER BY Date_tmp ASC;
DROP Table Calendar_tmp;