Forum Discussion

NPC's avatar
NPC
Icon for Helper I rankHelper I
3 years ago
Solved

Extract specific numbers from a group

Hello,

 

Hope you can help with this.

 

I have a table that looks like this:

Transaction IDExtracts
1021, 003, 022, 603, 415
2002, 001, 408, 510
3005, 004, 006, 510, 415, 408

 

From the Extracts column, I care about the last number (to the right) if its one of the following: 001, 002, 003, 004, 005, 006. All the other ones are irrelevant. 

 

So at the end, for Transcation ID 1, the number I would need is 003. 

For Transcation ID 2, 001.

For Transcation ID 3, 006.

 

The order of the number is very important and need to be mantained. And as you can see, there is no easy way to predict how many each Transaction ID will have. It could be 5, 1 or even up to 10. 

 

Thanks  you!

  • Hi NPC,

     

    You can extract these specific numbers from a group by using list functions.

     

    Create a custom column using this M Code:

     

     

    Extract = List.Last(List.Select(Text.Split([Extracts],","), each _ <= "006"))

     

     

    This code will:

    1. Convert your group into a list.
    2. Filter out everything except {001..006}.
    3. Return the right most element from that list.

     

    In case you don't have any number from 001 to 006 in your group, you'll get a null value.

     

     

    Works for you? Mark this post as a solution if it does!

6 Replies

  • Shaurya's avatar
    Shaurya
    Icon for Memorable Member rankMemorable Member

    Hi NPC,

     

    You can extract these specific numbers from a group by using list functions.

     

    Create a custom column using this M Code:

     

     

    Extract = List.Last(List.Select(Text.Split([Extracts],","), each _ <= "006"))

     

     

    This code will:

    1. Convert your group into a list.
    2. Filter out everything except {001..006}.
    3. Return the right most element from that list.

     

    In case you don't have any number from 001 to 006 in your group, you'll get a null value.

     

     

    Works for you? Mark this post as a solution if it does!

    • NPC's avatar
      NPC
      Icon for Helper I rankHelper I

      Shaurya Thank you! It works it some cases but not in all for some reason. As you can see below, instead of pulling 004, it pulled 209. Any thoughts as to why that's happening? The new column I created is " Exracted Latest Scenarion". Ignore the date column. 

       

      Thanks!

      • Shaurya's avatar
        Shaurya
        Icon for Memorable Member rankMemorable Member

        Hi NPC,

         

        I think this is because of the square brackets. The original column in your post had only comma separated values. However in the second case where you got the wrong output, the values are separated by comma but also enclosed in brackets. Can you check if you get any wrong values at all in the first case?

         

        As far as the second case is concerned, if you can specify the different ways in which the Extract column may have values, we can work out a solution that gives the right values in all cases.

  • I'll remove the square brackets and see what happens. Thanks!

      • NPC's avatar
        NPC
        Icon for Helper I rankHelper I

        The square brackets were causing issues. Thank you so much!