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

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.

Related

Add previous month column DAX Power BI

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

Power BI - Filter table using slicer to match within concatenated column

I've looked all over and can't find a way to do this.
In the table, I have all postcodes throughout the UK and a calculated column that concatenates from another table the products' that have been purchased in that location.
I need to filter the table to hide rows where the value selected in the slicer is in the concatenated column. I think this needs to be a measure and have tried using CONTAINSSTRING but nothing seems to be working.
Latest measure that I have tried is:
=IF(CONTAINSSTRING([Concatenated Values],[Selected Slicer Value]),"Hide","Show")
Does anyone have any ideas?
Example tables and expected results:
You can link the "another table" (the one with the products (not concatenated) per area) to the Area table.
Just change the filter option to: cross-filter direction: both
Then you can use it in your slicer.
Follow these below steps to achieve your requirement.
Let-
Your Slicer Table name: Table1
Your Details table name: Table2
Step-1: Create this following measure in Table2
show_hide_this_row =
FIND(
SELECTEDVALUE(Table1[products]),
MIN(Table2[products]),
,
0
)
Step-2: Add visual level filter using measure "show_hide_this_row" as below-
The output will be as below-
This functionality only works perfect when single selection is enabled in your slicer.

PowerBI how to use one slicer with two columns (dates)

I have a table with columns 'date from' and 'date to', and visualize them as bars using asTimeline visual.
I want to add a slicer which will work on both of these fields simultaneously. Currently I have two slicers working independently on each of those fields:
This is not really intuitive. Since start and end both define a period in time, if the period is inside the slider selection, it should be included. But slicers work on only one field. So I probably need to perform some DAX magic to create a field based on those two, but I don't know where to begin.
First create an measure to check a row overlaps the your date range:
Date Included =
IF (
FIRSTNONBLANK ( DateTable[Start Date], 1 ) <= MAX ( 'Calendar'[Date] ) &&
FIRSTNONBLANK( DateTable[End Date], 1 ) >= MIN ( 'Calendar'[Date] ),
"Include",
"Exclude"
)
and , add above Measure as a filter on your visualisation, where Date Included is Include
Than you can filter your Calendar table to single value, or range.
Also,only overlapping rows from your fact table will be displayed.
The problem can be solved by using a separate calendar-table and a measure: PowerBI filter- selected Date between Start and End date
Another way to solve this, and keep using the two slicers more intuitively, is described here: https://radacad.com/from-and-to-date-slicers-in-power-bi-filtering-based-on-two-fields. The solution describes how to set the properties of those timeline-slicers so that it makes a bit more sense for the user (set the "Start of employment" slicer as Type - After and the "End of employment" as Type - Before).

PowerBI filter- selected Date between Start and End date

I'm building a PowerBI report from a dataset that contains start and end dates. I'd like to filter the dataset based on rows that would encompass a selected date in another table.
The screenshot here shows a sample. I want to click a date in the table on the right and have the table on the left filtered where the selected date is between the start & end date.
I've attempted several different things using columns and measures, but I haven't been able to nail it down. I also attempted to create a new table from a DAX expression that references the selected date, but that caused errors.
How can I dynamically filter the dataset based on the selected date being between the start and end date values?
Create a measure to check whether a row overlaps the selected date range:
Date Included =
IF (
FIRSTNONBLANK ( Table1[Start Date], 1 ) <= MAX ( 'Calendar'[Date] ) &&
FIRSTNONBLANK( Table1[End Date], 1 ) >= MIN ( 'Calendar'[Date] ),
"Include",
"Exclude"
)
Add this Measure as a filter on your visualisation, where Date Included is Include
Now you can filter your Calendar table ( to single value, or range), and only overlapping rows from your fact table will be displayed.
See https://pwrbi.com/so_55925954/ for worked example PBIX file

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)