Forum Discussion

ReadTheIron's avatar
ReadTheIron
Icon for Helper III rankHelper III
4 years ago
Solved

Filtering a table on multiple criteria

I have two related tables, AssetTable and IncidentTable. I am trying to get a count of entries in FailureTable that satisfy multiple criteria. Specifically, I want to create a calculated column to count entries after a given date and excluding a given cause. Here, I want to count every incident except snow incidents after the asset was repaired.

 

Data examples:

AssetTable

AssetRepairDate
Switch12/2/2022
Switch22/3/2022
Switch32/4/2022

 

IncidentTable

AssetIncidentDateCause
Switch11/1/2022Snow
Switch11/12/2022Wind
Switch22/4/2022Fog
Switch12/4/2022Fog
Switch32/15/2022Snow
Switch12/20/2022Snow
Switch12/25/2022Wind

 

Desired table

AssetDate RepairedQualifyingIncidentsAfterRepair
Switch12/2/20222
Switch22/3/20221
Switch32/4/20220

 

My current calculated column only counts the incidents after the repair.

 

IncidentsAfterRepair =
IF(ISBLANK('AssetTable'[RepairDate]),0,
COUNTX(
    FILTER(
        RELATEDTABLE(IncidentTable),'AssetTable'[RepairDate]<=IncidentTable[IncidentDate]),IncidentTable[IncidentDate])+0)
  • Hi,

    This calculated column formula will work

    =coalesce(CALCULATE(COUNTROWS(Incident),FILTER(Incident,Incident[Asset]=EARLIER(Asset[Asset])&&Incident[IncidentDate]>EARLIER(Asset[RepairDate])&&Incident[Cause]<>"snow")),0)

    Hope this helps.

7 Replies

  • Hi,

    This calculated column formula will work

    =coalesce(CALCULATE(COUNTROWS(Incident),FILTER(Incident,Incident[Asset]=EARLIER(Asset[Asset])&&Incident[IncidentDate]>EARLIER(Asset[RepairDate])&&Incident[Cause]<>"snow")),0)

    Hope this helps.

    • ReadTheIron's avatar
      ReadTheIron
      Icon for Helper III rankHelper III

      I'm writing this column for other asset tables, which I believe are formatted exactly the same way, but I'm getting "The first argument of EARLIER/EARLIEST is not a valid column reference in the earlier row context". I can't figure out why it would work on one and not the other. Any thoughts?

      • ReadTheIron's avatar
        ReadTheIron
        Icon for Helper III rankHelper III

        Never mind, found my error - the AssetRepairDate being referred to was a measure not a column. Changed it from a measure to a calculated column and now everything is working.

  • You were close.

    IncidentsAfterRepair = 
    IF(ISBLANK([RepairDate]),0,
    COUNTX(FILTER(RELATEDTABLE(IncidentTable),[RepairDate]<=[IncidentDate] && [Cause]<>"Snow" ),1)+0)

     

    Are you sure you want this as a calculated column though?

    • ReadTheIron's avatar
      ReadTheIron
      Icon for Helper III rankHelper III

      I'm getting "too many arguments passed to the FILTER function. The maximum argument count for the function is 2"

       

      I will eventually want to use totals from this, so I was looking for a column instead of a measure - if a measure would make more sense, I can give that a try.