Add previous month column DAX Power BI - powerbi

I have a fact table which has 'Last Data Update' column that shows current month date(mm/dd/yyyy), 06/13/2022.
I am trying to add column called 'report month' that returns a value that shows previous month/year 05/2022 to create relationship with calendar table.
=Date.Month([Last Data Update])-1
I have used this code, but this only returns 5 and it is in number format.
is there a way to return 05/2022 in DAX ?
Thanks in advance.

This is M, not DAX. Replace your custom column with the following code:
Text.PadStart( Text.From(Date.Month([Last Data Update])-1),2,"0") &"/"& Text.From(Date.Year([Last Data Update]))

It worked for we with this 3 simple steps:
Step 1: Create a column with current year (True/False statement) by using:
CurrentYear = IF(YEAR([Last Data Update])=YEAR(NOW()),1,0)
Step 2: Create a column that will indicate if the current date (based on column "Last Data Update") belongs to previous month or not (True/False statement).
PreviousMonth = IF([CurrentYear]=1 && (MONTH(TODAY())-1)=MONTH([Last Data Update]),1,0)
Step 3: Create a filter on your visual where you select filter "PreviousMonth" with value "1" to show dates only from previous month).
If you like it. Don't forget to rate it.
Success,
Mark

This month-year
FORMAT( [Last Data Update], "mm/yyyy")
previous month
FORMAT( DATEADD([Last Data Update],-1,Month), "mm/yyyy")

Related

Power BI Dax Expression Help - Setting default slicer between dates

Power Bi report uses a date slicer (between) - As a default I want it to set the first date in the slicer as Monday (last week) and the last date as Friday (last week). I dont want this to be a relative date but always must be the previous Mon-Fri. I have a column in my data set that tells me which one is the 'Previous Week' as a text field. I dont know how to use this to set a default slicer value. But i also want to make sure the user can change the slicer dates and it changes as per the user request.
Example today is 01/09/2020
I want my default slicer date as : 24/08/2020 - 28/08/2020
I have two measures which gives me by last week dates:
DefaultSD = MINX( FILTER(Data, [WeeksFilter] = "Previous Week Only"),[Date])
DefaultED = MAXX( FILTER(Data, [WeeksFilter] = "Previous Week Only"),[Date])
I assume this is possible using a DAX expression but dont know how to create this expression to populate these default values.
Please note the data is refreshed up until 01/09/2020 so there is currently data for this week but i dont want this to included in the default slicer on report load
You can't set slicer defaults yet but this can be achieved with relative date filtering.
Select date in the last 1 calendar week:

New column indicating if row is the first instance of the value for the Entity ID in PowerQuery Editor

I currently have a column that is created using the following DAX formula which indicates if the listed activity is the first one ever for that Entity ID:
First Time Activity =
if('Activity Table'[Timestamp]=
CALCULATE(min('Activity Table'[Timestamp]),
filter('Activity Table',
'Activity Table'[Entity ID] = earlier('Activity Table'[Entity ID]) &&
'Activity Table'[Activity Name] = earlier('Activity Table'[Activity Name])
)
)
,1,BLANK())
But I need this now to be a column made in PowerQuery instead of DAX. Any help on the PowerQuery formula would be much appreciated.
Thanks
In the query editor, you can do the following steps:
Group by ID and Activity taking the min over the Date column as its aggregation.
Merge that grouped table back onto the original table (matching on ID and Activity and using left outer join).
Expand the min date column.
On the merged and expanded table, replace non-nulls in the expanded column with 1.

PowerBI: SAMEPERIODLASTYEAR function not working

I am getting the following error message when I write the below code:
Value for 'Total Sales'cannot is determined. Either 'Total Sales' does not exist or there is no current row for a column named 'Total Sales'
enter code here
LYSales = CALCULATE([Total Sales],SAMEPERIODLASTYEAR('Date'[Date]))
How do I fix this? I have the Total Sales column with data. It is set to general currency.
Written in this way your formula is not referencing a measure, not a column, also you will need an expression inside calculate.
-- This reference a Measure, which in your case is not defined
LYSales = CALCULATE([Total Sales],SAMEPERIODLASTYEAR('Date'[Date]))
-- you can create it with this formula
Total Sales = SUM('_TableName_'[Total Sales])
--The other way is to use only one formula
LYSales = CALCULATE(SUM('_TableName_'[Total Sales]),SAMEPERIODLASTYEAR('Date'[Date]))
As a note, to use time intelligence functions you need to mark the calendar table as "date table": right click on the table -> "Mark as Date Table"
The calculation looks at the table column as a whole. Your formula uses the value in the current row of the column. So, change it to
LYSales = CALCULATE('Your Table Name'[Total Sales],SAMEPERIODLASTYEAR('Date'[Date]))

Need to limit the date in a slicer by today's date in Power BI

I am building a report in Power BI Desktop, created a slicer - YearMonthSort - which has data selection by Year-Month
Plz, see the screenshot below:
My goal is to limit data in this slicer as -
2015-07 to today's date
(whichever it will be when users will look at the data,
in the same format - "YYYY-MM")
In the "Filters" section I can select my starting year as 2015-07,
but having problem setting today's date.
I tried to create the new Measure, but not sure where to place it,
or, may be there is another way to perform this:
IsToday = FORMAT(TODAY(), "mm/yyyy")
I only just started to learn this,
Thank you for help in advance!
You can do the following:
I am assuming that you are using a calendar table that is joined to your fact table on a date field and that your year sort column is calculated with reference to the 'date' field in your calendar table.
Assuming, this is true you can create a calculated column that will flag whether or not the date field is before or after today. This can be achieved by using the following DAX:
IsToday =
SWITCH (
TRUE (),
'Calendar'[Date]
<= NOW (), 1,
0
)
Then, in your visual add a 'Visual Level Filter' using this new column and set it to 1.

Split single row of data in Power BI data source into multiple rows

I have a table in a Power BI data source with a column for term start and term end date (term length can be longer than a month), along with meta data on the term. I need to report on status of purchased terms as at the end of each month. As far as I can see, the best way of accomplishing this would be to create a calculated table with an entry for each month on which a term is active at its end.
For example, an entry in the original table with the following data:
TermStartDate TermEndDate PurchaseAmount
2018-01-03 2018-04-12 100
Would end up in the calculated table as follows:
MonthPurchased PurchaseAmount
2018-01 100
2018-02 100
2018-03 100
How to accomplish this? Is there a better way than creating a separate calculated table to get this data? Any help or advise is appreciated
I managed to solve this myself, I detail the required steps below for reference:
Change start and end date column data types from Datetime to Date. <- This is needed to ensure we only generate dates on day boundaries in the next step
Add custom column with the following formula:
Month = List.Select( List.Dates([TermStartDate], Number.From([TermEndDate] - [TermStartDate]) +1, #duration(1, 0, 0, 0)), each _ = Date.EndOfMonth(_) )
This generates a list of dates between start and end, then filters to only leave the dates that are at the end of a month
Expand to new rows on the new Month column (menu at the top of the column)
Use Detect Data Type option on the Month column to change the datatype from Any to Date (for some reason I cannot manually select Date, the DataType menu option is greyed out on the Month column)