Forum Discussion
How to pull date from another table based on complex if statement
Hello,
I am working on trying to be able to pull a date in from another table to multiple columns depending on the logic that is listed below. I have the example data sets below as well along with what we would expect to look like.
- IBT -1: Pull first occurrence of date. if field isblank and Moderate <>"" or High <>"" or Critical <>"" then N/A
- IBT- 2: Pull second occurrence of date. if field isblank and Moderate <>"" or High <>"" or Critical <>"" then N/A
- IBT- 3: Pull third occurrence of date. if field isblank and Moderate <>"" or High <>"" or Critical <>"" then N/A
- Moderate: Pull fourth occurrence of date or if "Scoring Category" = "Moderate". if field isblank and High <>"" or Critical <>"" then N/A
- High: Pull fifth occurrence of date or if "Scoring Category" = "High". if field isblank and Critical <>"" then N/A
- Critical: Pull sixth occurrence of date or if "Scoring Category" = "Critical"
Raw Data Source
| ID # | ACTIVITY_DATE | SCORING CATEGORY | EVENT |
| 88197 | 6/10/2024 | 1 | |
| 37 | 6/18/2024 | 1 | |
| 50507 | 7/1/2024 | CRITICAL | |
| 88197 | 7/3/2024 | 1 | |
| 88197 | 7/5/2024 | 2 | |
| 37 | 7/5/2024 | MODERATE | |
| 50507 | 7/5/2024 | 2 | |
| 88197 | 7/8/2024 | 1 | |
| 88197 | 7/12/2024 | 1 | |
| 88197 | 7/30/2024 | 1 | |
| 37 | 8/1/2024 | 1 | |
| 88197 | 8/1/2024 | 1 | |
| 50507 | 8/5/2024 | Moderate |
Expected outcome in new table
| ID # | Location | Status | Name | Type | IBT-1 | IBT-2 | IBT-3 | Moderate | High | Critical |
| 88197 | 1 | A | Test 1 | N | 6/10/2024 | 7/3/2024 | 7/5/2024 | 7/8/2024 | 7/12/2024 | 7/30/2024 |
| 37 | 2 | A | Test 2 | CM | 6/18/2024 | N/A | N/A | 7/5/2024 | 8/1/2024 | |
| 50507 | 3 | A | Test 3 | CD | N/A | N/A | N/A | N/A | N/A | 7/1/2024 |
There are some rows of data in the first table that should not be reflected in the new table as they would not be applicable due to them already meeting the final column "Critical".
I have been banging my head on this one for a bit as this is a pretty complex request, and I feel like I have the logic sound as well as what I could do, but I think I am just missing some threads to tie it all together.
Create a calculated column to get the occurrence
Occurrence Order = VAR CurrentID = 'Table'[ID] VAR CurrentDate = 'Table'[ACTIVITY_DATE] RETURN COUNTROWS( FILTER( 'Table', 'Table'[ID] = CurrentID && 'Table'[ACTIVITY_DATE] <= CurrentDate ) )and create another calculated column to to do the grouping
New Column 2 = VAR OccurrenceNumber = 'Table'[Occurrence Order] VAR ScoringCategory = 'Table'[SCORING CATEGORY] RETURN SWITCH( TRUE(), OccurrenceNumber = 4, "Moderate", OccurrenceNumber = 5, "High", OccurrenceNumber = 6, "Critical", OR(ISBLANK(ScoringCategory), ScoringCategory = ""), "IBT-" & OccurrenceNumber, ScoringCategory )and use a matrix table and add the following fields
7 Replies
- kushanNaSuper User
Create a calculated column to get the occurrence
Occurrence Order = VAR CurrentID = 'Table'[ID] VAR CurrentDate = 'Table'[ACTIVITY_DATE] RETURN COUNTROWS( FILTER( 'Table', 'Table'[ID] = CurrentID && 'Table'[ACTIVITY_DATE] <= CurrentDate ) )and create another calculated column to to do the grouping
New Column 2 = VAR OccurrenceNumber = 'Table'[Occurrence Order] VAR ScoringCategory = 'Table'[SCORING CATEGORY] RETURN SWITCH( TRUE(), OccurrenceNumber = 4, "Moderate", OccurrenceNumber = 5, "High", OccurrenceNumber = 6, "Critical", OR(ISBLANK(ScoringCategory), ScoringCategory = ""), "IBT-" & OccurrenceNumber, ScoringCategory )and use a matrix table and add the following fields
- PhillipC1Helper I
Hi kushanNa,
This worked great, but is there a way to limit the occurence number to never exceed 3? When I run this with a larger data set there are some occurences that come up at 7 or 8. As the source data set grows overtime I think that will continue to increase.
- kushanNaSuper User
Hi , add a >= 6 then it will stop from 6 , and change the matrix table values to latest so it will show the last occurrence date
New Column 2 = VAR OccurrenceNumber = 'Table'[Occurrence Order] VAR ScoringCategory = 'Table'[SCORING CATEGORY] RETURN SWITCH( TRUE(), OccurrenceNumber = 4, "Moderate", OccurrenceNumber = 5, "High", OccurrenceNumber >= 6, "Critical", OR(ISBLANK(ScoringCategory), ScoringCategory = ""), "IBT-" & OccurrenceNumber, ScoringCategory )
- lbendlinSuper User
if field isblankplease elaborate. What field?
Your sample data is missing the Location, Status, NameType columns. Are these not important?
Note that Power Query is case sensitive. MODERATE is not the same as Moderate. (doesn't matter in DAX)
- PhillipC1Helper I
If field is blank or more stating that if that field, let's use IBT-1 for example, is blank. Essentially this is a stair step process meaning if you show up on this report the date for the row you show up on will go to IBT-1, then if you show up again you go to IBT-2, etc. The exception is if you have the result of "Moderate", "High", or "Critical" as a value under "Scoring Category" then you would skip all previous steps meaning they would result in "N/A".
The data points not provided (location, name, etc.) are not important for this example as my concern is more so around how to get the logic for the remaining fields.
Whoops, yep, sorry I wrote the example wrong but the actual format will match in prod.