Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Danny26
Frequent Visitor

How to split Azure resource tags in to separate columns.

Hi All,

After much research and trial and error I am trying to figure out a way to parse the Azure resource tags that are exported using the Azure Enterprise connector and use them against other metrics for cost analysis etc. I know you can split the column "Tags" based on a delimiter and assuming the correct tags are applied in the same order for each resource in to separate column's but in our environment the number of tags applied could vary. Below is an example of how the tags are displayed in the exported table.

{"Hostname":"TestSvr01","Location":"Sydney","Project":"12345","Workload":"TestApp1","Workorder":"1234"}

 

What I am trying to achieve is a script or a query which would search for a key word in the Tags column and then place that value into a separate column e.g. Search for the keyword Hostname and place the value next to it in a new column called hostnames. Because a resource/VM could have multiple "Workload" tags I need a method to parse the string instead of using straight delimiters to column process.
Any help with this would be very much appreciated and please ask me to clarify if anything is unclear.
On a side note I am very surprised that MS make reporting on resource tags so difficult.

Thanks
Danny

2 ACCEPTED SOLUTIONS

Hi @v-ljerr-msft

 

Sorry for the multiple posts but I think I have had some success. The code below which includes an IF statement at the start does seem to do the trick and doesn't error out when it can not find the key word. It may not be pretty but it seems to do the job, if you could offer any further refinements that would be great but otherwise I think we might have cracked it. 

 

Hostname = 
IF(SEARCH( "Hostname" ,'Table1-Full'[Tags],,BLANK() ) ,(
VAR textLength =
    SEARCH (
        ",",
        'Table1-Full'[Tags],
        SEARCH ( "Hostname", 'Table1-Full'[Tags] ) + LEN ( "Hostname" )
            + 3
    )
        - (
            SEARCH ( "Hostname", 'Table1-Full'[Tags] ) + LEN ( "Hostname" )
                + 3
        )
        - 1
RETURN
    MID (
        'Table1-Full'[Tags],
        SEARCH ( "Hostname", 'Table1-Full'[Tags] ) + LEN ( "Hostname" )
            + 3,
        textLength
    )))

 

Thanks

Danny

View solution in original post

Hi @v-ljerr-msft

 

Thank you for your help with developing this solution. I have updated the code slightly to cater for the fact that when a particular Tag I am searching for is at the end of the text string the code would fail because it can't find the last delimitated character ",". So I have changed the delimited character to be ". Now any tag of any length and position within the string can be found and separated out into its own column. e.g. Workload, Location, Hostname. Hopefully this code can be used by others who are trying to report against Azure resource tags. 

 

Hostname = 
IF(SEARCH( "Hostname" ,'All-Azure-Data'[Data.Tags],,BLANK() ) ,(
VAR textLength =
    SEARCH (
        """",
        'All-Azure-Data'[Data.Tags],
        SEARCH ( "Hostname", 'All-Azure-Data'[Data.Tags] ) + LEN ( "Hostname" )
            + 3
    )
        - (
            SEARCH ( "Hostname", 'All-Azure-Data'[Data.Tags] ) + LEN ( "Hostname" )
                + 3
        )
        
RETURN
    MID (
        'All-Azure-Data'[Data.Tags],
        SEARCH ( "Hostname", 'All-Azure-Data'[Data.Tags] ) + LEN ( "Hostname" )
            + 3,
        textLength
    )))

 

 

 

View solution in original post

8 REPLIES 8
v-ljerr-msft
Employee
Employee

Hi @Danny26,

 

If I understand you correctly, you should be able to use MID and FIND DAX function to create new calculate columns under Modeling tab to search for a key word in the Tags column and then place that value into a separate column in your scenario. The formula below is for your reference. Smiley Happy

 

Hostname =
MID (
    Table1[Tags],
    FIND ( "Hostname", Table1[Tags] ) + LEN ( "Hostname" )
        + 3,
    LEN ( "TestSvr01" )
)

hostname.PNG

 

Regards

Hi @v-ljerr-msft

 

Thank you very much for the reply you have certainly giving me that spark I needed. I have added to the code an IF statement so if the key word is not found in the particular row e.g. the row which contains all the Azure tag information then do not perform the operation otherwise I get text in there that I don't want. 

 

Hostname = 
IF(FIND("Hostname",'Table1'[Tags],,BLANK()),
MID ( 'Table1'[Tags], FIND("Hostname",Table1'[Data.Tags],,BLANK()) + LEN ( "Hostname" ) + 3, LEN ( "TestSvr01" ) ),"")

 

I would like to add a further bit of logic which caters for a hostname of variable length.  I was going about this by doing another FIND command from the position determined by the first FIND command and locate the end position of the delimited character in this case a comma ",". Once I had that number I was going to subtract the number of characters that make up the word "Hostname" to give me the total number of characters the hostname represents.   So TestSvr01 would return 9 characters and Server01 would return 8 characters. With the correct number of characters found I was going to pass it to the MID command so it only extracts the right amount of text e.g. only the hostname and no trailing characters.

 

I understand that you cannot pass a variable or an operation in <start_num> section of a FIND command as per FIND(<find_text>, <within_text>[, [<start_num>][, <NotFoundValue>]])  because this command doesn’t work

FIND(",",'Table1'[Tags],FIND("Hostname",'Table1'[Tags],,BLANK()),BLANK())

 

so I am a little bit stumped as to how to calculate the correct number characters in a hostname and pass that to a MID command and extract only the hostname so any further assistance would be greatly appreciated.  Also if you know of any easier way to achieve the same result say with the Power Query command "Text.PositionOf" that would be great as well. If we can solve this last outstanding question then my reports back to management would be complete.

 

Thanks again!

Danny

Hi @Danny26,

 

Try the formula below to see if it works in your scenario. Smiley Wink

 

Hostname = 
VAR textLength =
    FIND (
        ",",
        Table1[Tags],
        FIND ( "Hostname", Table1[Tags] ) + LEN ( "Hostname" )
            + 3
    )
        - (
            FIND ( "Hostname", Table1[Tags] ) + LEN ( "Hostname" )
                + 3
        )
        - 1
RETURN
    MID (
        Table1[Tags],
        FIND ( "Hostname", Table1[Tags] ) + LEN ( "Hostname" )
            + 3,
        textLength
    )

cc.PNG

 

Regards

Hi @v-ljerr-msft

 

Thank you for the code block. When I copy it across and update the table names it doesn't strictly work straight away. For reasons which I am not quite sure about any FIND command I use in Power BI desktop (which is the current April 2017 version) doesn't work unless I include ,,BLANK() on the end otherwise I get the error "The search Text provided to the function 'FIND' could not be found in the given text' so I have amended the code as per below. So it appears that all the FIND commands work now but the MID command does not. 

 

I am presented with the error 'An argument of function 'MID' has the wrong data type or has an invalid value' so I am assuming this is because we are trying to pass it the variable name 'textLength' as the command works when I replace that with a number typed in by myself, my understanding was you couldn't pass a variable to the MID command. I'm sure the code block works when you constructed your example so I am not sure what the difference between the two could be, do you think I changed how the code executed by adding ,, BLANK() to the end of the FIND command.  

  

Hostname = 
VAR textLength =
    FIND (
        ",",
        TestBatch4[Tags],
        FIND ( "Hostname", TestBatch4[Tags],,BLANK()) + LEN ( "Hostname" )
            + 3
    ,BLANK())
        - (
            FIND ( "Hostname", TestBatch4[Tags],,BLANK()) + LEN ( "Hostname" )
                + 3
        )
        - 1
RETURN
    MID (
        TestBatch4[Tags],
        FIND ( "Hostname", TestBatch4[Tags],,BLANK()) + LEN ( "Hostname" )
            + 3,
        textLength
    )

 

Once again thank you very much for your help and I certainly do like how this solution is being solved.

 

Regards

Danny 

Hi @v-ljerr-msft

 

After a bit more trial and error I have discovered the reason why the original code block doesn't work for me. I have noticed in my data set that I have rows which don't contain the key word hostname and thus the code returns the error 'FIND' could not be found in the given text'. As soon as I have a data set which contains 'Hostname' on every row the code works. I certainly appreciate your help so far so is it possible to wrap up the Find/Search commands with an IFERROR command so it skips over the rows that don't contain the key word. A sample dataset is below, apologies once again for not being to up front with the lay out of the entire data I have only just started out with Power BI. 

 

{"WorkOrder":"6743","Hostname":"Srv04","Project":"1234AB","Location":"Sydney","Workload":"TestApp1"}

{"Hostname":"TestMgmt01","Location":"Sydney","Project":"8015JF","Workload":"TestApp2","Workorder":"7732"}

{"Hostname":"TestNPR01","Location":"Sydney","Project":"1234AA","Workload":"TestApp3","Workorder":"3315"}

{"WorkOrder":"6743","Hostname":"TestSvr02","Project":"1234AB","Location":"Sydney","Workload":"TestApp4"}

{"Hostname":"TestMgmt02","Location":"Sydney","Project":"4455CC","Workload":"TestApp2","Workorder":"7735"}

{"Hostname":"TestMgmt01","Location":"Sydney","Project":"8994TT","Workload":"TestApp2","Workorder":"7732"}

{"WorkOrder":"4467"}

{"Location":"Sydney"}

 

Thanks

Danny

 

Hi @v-ljerr-msft

 

Sorry for the multiple posts but I think I have had some success. The code below which includes an IF statement at the start does seem to do the trick and doesn't error out when it can not find the key word. It may not be pretty but it seems to do the job, if you could offer any further refinements that would be great but otherwise I think we might have cracked it. 

 

Hostname = 
IF(SEARCH( "Hostname" ,'Table1-Full'[Tags],,BLANK() ) ,(
VAR textLength =
    SEARCH (
        ",",
        'Table1-Full'[Tags],
        SEARCH ( "Hostname", 'Table1-Full'[Tags] ) + LEN ( "Hostname" )
            + 3
    )
        - (
            SEARCH ( "Hostname", 'Table1-Full'[Tags] ) + LEN ( "Hostname" )
                + 3
        )
        - 1
RETURN
    MID (
        'Table1-Full'[Tags],
        SEARCH ( "Hostname", 'Table1-Full'[Tags] ) + LEN ( "Hostname" )
            + 3,
        textLength
    )))

 

Thanks

Danny

Hi @Danny26,

 

Nice solution!

 

Could you accept your last reply as solution to help others who may also have the similar issue easily find the answer and close this thread? Smiley Happy

 

Regards

Hi @v-ljerr-msft

 

Thank you for your help with developing this solution. I have updated the code slightly to cater for the fact that when a particular Tag I am searching for is at the end of the text string the code would fail because it can't find the last delimitated character ",". So I have changed the delimited character to be ". Now any tag of any length and position within the string can be found and separated out into its own column. e.g. Workload, Location, Hostname. Hopefully this code can be used by others who are trying to report against Azure resource tags. 

 

Hostname = 
IF(SEARCH( "Hostname" ,'All-Azure-Data'[Data.Tags],,BLANK() ) ,(
VAR textLength =
    SEARCH (
        """",
        'All-Azure-Data'[Data.Tags],
        SEARCH ( "Hostname", 'All-Azure-Data'[Data.Tags] ) + LEN ( "Hostname" )
            + 3
    )
        - (
            SEARCH ( "Hostname", 'All-Azure-Data'[Data.Tags] ) + LEN ( "Hostname" )
                + 3
        )
        
RETURN
    MID (
        'All-Azure-Data'[Data.Tags],
        SEARCH ( "Hostname", 'All-Azure-Data'[Data.Tags] ) + LEN ( "Hostname" )
            + 3,
        textLength
    )))

 

 

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.