powerbidax

Can I extract a specific path in Power Bi with DAX?


Is there a way to extract a specific path from a string with DAX? I'm looking for a specific file in a specific subfolder structure. All paths where the file with this name exists are concatenated in one string separated by " | " (for formating purposes, I've switched it to ", " in the example below)

File All Paths Only specific Path
1234567.txt /Volumes/Media/xxx/Project/1234567/wrong/subfolder/1234567.txt, /Volumes/Media/xxx/Project/1234567/correct/subfolder/1234567.txt /Volumes/Media/xxx/Project/1234567/correct/subfolder/1234567.txt

Solution

  • We can leverage the Parent and Child functions to solve this in DAX. (It would better doing it in Power Query.)

    Try the following as a Calculated Column:

    thePath = 
      var fileName = "/1234567.txt"
      var filePath = "/correct/"
    
      var pipedPaths = [All Paths] // or SUBSTITUTE ( [All Paths], ", ", "|" ) if not pipe delimited
    
      var pathList =
        SELECTCOLUMNS(
          GENERATESERIES( 1, PATHLENGTH(pipedPaths) ),
          "path", TRIM( PATHITEM(pipedPaths, [Value]) )
        )
      
      // returns only one match
      RETURN
        MAXX(
          FILTER( pathList, CONTAINSSTRING([path], filePath) && CONTAINSSTRING([path], fileName) ),
          [path]
        )
    
      // or to return all matches with a delimiter
      /*
      RETURN
        CONCATENATEX(
          FILTER( pathList, CONTAINSSTRING([path], filePath) && CONTAINSSTRING([path], fileName) ),
          [path], ", "
        )
      */