Forum Discussion

Neiners's avatar
Neiners
Icon for Helper II rankHelper II
1 year ago

Need help with Substitute, pathlength, and generateseries to lookup values in another table

Need help using the SUBSTITUTE function to get the PATHLENGTH and GENERATESERIES to GENERATE a virtual table that is used to filter other tables within measures.

 

I need to reconcile assets among multiple tables based off of some computer properties such as computer name, asset id, serial number, mac address and/or ip address.

 

I start with these variables from the originating table that I need to see if any of those fields match in another table. In the example below, I have added the SUBSTITUTE function for those fields where I need to lookup the values separated by the delimiter UNICHAR(10) in another table.

 

The first problem that I am running into is if there is a blank in any of the Table1[Lookup] columns, the Pathlength will return a 1 even if it is blank. Do I make the _pathlengths a conditional statement? For example: _pathlength = IF(_macaddress = "", "0", PATHLENGTH(_macaddress)) which will result in a 0 instead of a 1. If I do this, it generates an error with the GENERATESERIES function "The arguments in GenerateSeries function must be of a numeric or date/time type." If I leave _pathlength = PATHLENGTH(_macaddress) then I get an error with the GENERATESERIES function "The arguments in GenerateSeries function cannot be blank.

 

I was using the following for those fields that only had one value in the variable to compare the OS for the asset.

 

OS Lookup (Table1) =
var _computername = SELECTEDVALUE(Table1[Computer Name])
var _AssetID = SUBSTITUTE(SELECTEDVALUE(Table1[AssetID Lookup]), UNICHAR(10), "|")
var _AssetIDpathlength = PATHLENGTH(_AssetID)
var _serialnumber = SUBSTITUTE(SELECTEDVALUE(Table1[Serial Number Lookup]), UNICHAR(10), "|")
var _serialnumberpathlength = PATHLENGTH(_serialnumber)
var _macaddress = SUBSTITUTE(SELECTEDVALUE(Table1[MAC Address Lookup]), UNICHAR(10), "|")
var _macaddresspathlength = PATHLENGTH(_macaddress)

 

//table 2 computer name lookup
var _table2computername = COUNTROWS(FILTER(Table2,Table2[Computer Name] = _computername))
var _table2computernamereturned =
IF(_computername = BLANK(), BLANK(),
IF(_table2computername <> BLANK(),
CONCATENATEX(FILTER(Table2,
Table2[Computer Name] = _computername), Table2[os], UNICHAR(10))))

//table 3 computer name lookup
var _table3computername = COUNTROWS(FILTER(Table3,Table3[Computer Name] = _computername))
var _table3computernamereturned =
IF(_computername = BLANK(), BLANK(),
IF(_table3computername <> BLANK(),
CONCATENATEX(FILTER(Table3,
Table3[Computer Name] = _computername), Table3[os], UNICHAR(10))))

//table
var _table =
FILTER(
{
_table2computernamereturned,
_table3computernamereturned
},
NOT ISBLANK([Value])
)

var _filtertable =
CONCATENATEX(DISTINCT(_table),[Value],UNICHAR(10))

RETURN
_filtertable

 

Because concatenatex returns a string, the distinct function was not working and the same value was being returned multiple times.


I then changed the code to reflect this:

OS Lookup (Table1) =
var _computername = SELECTEDVALUE(Table1[Computer Name])
var _AssetID = SUBSTITUTE(SELECTEDVALUE(Table1[AssetID Lookup]), UNICHAR(10), "|")
var _AssetIDpathlength = PATHLENGTH(_AssetID)
var _serialnumber = SUBSTITUTE(SELECTEDVALUE(Table1[Serial Number Lookup]), UNICHAR(10), "|")
var _serialnumberpathlength = PATHLENGTH(_serialnumber)
var _macaddress = SUBSTITUTE(SELECTEDVALUE(Table1[MAC Address Lookup]), UNICHAR(10), "|")
var _macaddresspathlength = PATHLENGTH(_macaddress)
var _ipaddress = SELECTEDVALUE(Table1[IP Address Lookup])

//table 2 computer name lookup
var _table2computername = FILTER(Table2,Table2[Computer Name] = _computername)

//table 3 computer name lookup
var _table3computername = FILTER(Table3,Table3[Computer Name] = _computername)

//table
var _table =
UNION(
SELECTCOLUMNS(_table2computername, "OS", Table2[OS]),
SELECTCOLUMNS(_table3computername, "OS", Table2[OS]),
)
var _distinct = DISTINCT(_table)
RETURN
CONCATENATEX(_distinct, [OS], UNICHAR(10))

 

This works great until I get to a lookup field that is split by a delimiter, especially those with blank values. I tried this

 

//Table 2 mac address lookup
var _table2macaddressreturned =
IF(SELECTEDVALUE(Table1[MAC Address Lookup]) <> BLANK(),
VAR _txt = VALUES(Table1[MAC Address Lookup])
var _txtPath = SUBSTITUTE(_txt, UNICHAR(10), "|")
var _txtPathlen = PATHLENGTH(_txtPath)
var _pathIndexes = GENERATESERIES(1, _txtPathlen, 1)
var _pathIndexesLookups =
GENERATE(_pathIndexes,
CALCULATETABLE(
VALUES(C_Table2macs[OS]),
TREATAS( { PATHITEM(_txtPath, NOT ISBLANK([Value]), TEXT) }, C_Table2macs[MAC Address])
)
)
RETURN
CONCATENATEX(_pathIndexesLookups, C_Table2macs[OS], UNICHAR(10)))

//Table 3 mac address lookup
var _table3macaddressreturned =
IF(SELECTEDVALUE(Table1[MAC Address Lookup]) <> BLANK(),
VAR _txt = VALUES(Table1[MAC Address Lookup])
var _txtPath = SUBSTITUTE(_txt, UNICHAR(10), "|")
var _txtPathlen = PATHLENGTH(_txtPath)
var _pathIndexes = GENERATESERIES(1, _txtPathlen, 1)
var _pathIndexesLookups =
GENERATE(_pathIndexes,
CALCULATETABLE(
VALUES(C_Table3macs[OS]),
TREATAS( { PATHITEM(_txtPath, NOT ISBLANK([Value]), TEXT) }, C_Table3macs[MAC Address])
)
)
RETURN
CONCATENATEX(_pathIndexesLookups, C_Table3macs[OS], UNICHAR(10)))

//table
var _table =
FILTER(
{
_table2computernamereturned,
_table3computernamereturned
},
NOT ISBLANK([Value])
)

var _filtertable =
CONCATENATEX(DISTINCT(_table),[Value],UNICHAR(10))

RETURN
_filtertable

 

This works, but again it is returning the same OS for each of the PathItems so I think I need to create a virtual table with the OS that is returned from the mac addresses and then filter out the distinct values similar to what was done with the computer name lookup. The other thing I would need to do is combine both the computer name lookups and the mac address lookups into the same measure and only return distinct values and that is where I am completely stuck. I tried

 

OS Lookup (Table1) =
var _computername = SELECTEDVALUE(Table1[Computer Name])
var _AssetID = SUBSTITUTE(SELECTEDVALUE(Table1[AssetID Lookup]), UNICHAR(10), "|")
var _AssetIDpathlength = PATHLENGTH(_AssetID)
var _serialnumber = SUBSTITUTE(SELECTEDVALUE(Table1[Serial Number Lookup]), UNICHAR(10), "|")
var _serialnumberpathlength = PATHLENGTH(_serialnumber)
var _macaddress = SUBSTITUTE(SELECTEDVALUE(Table1[MAC Address Lookup]), UNICHAR(10), "|")
var _macaddresspathlength = PATHLENGTH(_macaddress)
var _ipaddress = SELECTEDVALUE(Table1[IP Address Lookup])

//table 2 computer name lookup
var _table2computername = FILTER(Table2,Table2[Computer Name] = _computername)

//table 3 computer name lookup
var _table3computername = FILTER(Table3,Table3[Computer Name] = _computername)

//table 2 mac address lookup
var _table2mac address =
FILTER(C_Table2macs, C_Table2macs[MAC Address] =
GENERATE(_macaddressgenerateseries,
CALCULATETABLE(
VALUES(C_Table2macs[OS]),
TREATAS({PATHITEM(_macaddresssubstitute, NOT ISBLANK([Value]),TEXT)}, C_Table2macs[MAC Address])
)
)
)

//table3 mac address will do the same as _table2macaddress

 

//table
var _table =
SELECTCOLUMNS(_table2computername, "OS", Table2[OS]),
SELECTCOLUMNS(_table3computername, "OS", Table2[OS]),
SELECTCOLUMNS(_table2macaddress, "os", _macaddressgenerateseries)
)
var _distinct = DISTINCT(_table)
RETURN
CONCATENATEX(_distinct, [OS], UNICHAR(10))

8 Replies

  • please give us something to work with.  Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).
    Do not include sensitive information. Do not include anything that is unrelated to the issue or question.
    Please show the expected outcome based on the sample data you provided.

    Need help uploading data? https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-p/963216
    Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523

    • Neiners's avatar
      Neiners
      Icon for Helper II rankHelper II

      thanks, I will try to create a sample pbx. My data has sensitive information which is why I tried to generalize the code. I basically have up to 5 fields that I use to look up those values in other tables (Computer Name, AssetID, Serial Number, MAC Address and IP Address). Using the MAC Address as an example, an asset may have no mac addresses assigned to it or it may have multiple mac addressess assigned to it. Each MAC Address assigned to an asset is seperated by the UNICHAR(10) delimiter to make it easier to read on reports. I would like to take those mac addresses seperated by a delimiter and look up each one in another table. The problem is some assets don't have a MAC address assigned to them so it is causing errors when using the GENERATESERIES function.  This is what I have for the mac address lookup:

      MAC Address Lookup = 

      var _macaddress = SELECTEDVALUE(Table1[MAC Address Lookup]) //This may be blank or it may have multiple mac addresses seperated by UNICHAR(10)

       

      var _table2macaddressreturned =
      IF(SELECTEDVALUE(table1[MAC Address Lookup]) <> BLANK(),
      VAR _txt = VALUES(table1[MAC Address Lookup])
      var _txtPath = SUBSTITUTE(_txt, UNICHAR(10), "|")
      var _txtPathlen = PATHLENGTH(_txtPath)
      var _pathIndexes = GENERATESERIES(1, _txtPathlen, 1)
      var _pathIndexesLookups =
      GENERATE(_pathIndexes,
      CALCULATETABLE(
      VALUES(table2[OS]),
      TREATAS( { PATHITEM(_txtPath, [Value], TEXT) }, table2[MAC Address])
      )
      )
      RETURN
      CONCATENATEX(_pathIndexesLookups, table2[os], UNICHAR(10)))


      //table
      var _table =
      FILTER(
      {
      _table2macaddressreturned
      },
      NOT ISBLANK([Value])
      )

      var _filtertable =
      CONCATENATEX(DISTINCT(_table),[Value],UNICHAR(10))

      RETURN
      _filtertable

       

      The problem that I am facing is those assets with blank mac addresses and those assets that have multiple mac addresses where the OS is returned for however many pathitems there are. So if an asset has 2 or more Macs that were matched, then it would return back the OS for both of those mac addresses. I would like to get a distinct list of the OS.

      • lbendlin's avatar
        lbendlin
        Icon for Super User rankSuper User

        Please provide sample data that fully covers your issue.
        Please show the expected outcome based on the sample data you provided.

  • v-pnaroju-msft's avatar
    v-pnaroju-msft
    Icon for Community Support rankCommunity Support

    Hi Neiners,

    We kindly request you to provide sample data that accurately represents your issue or query in an appropriate format, excluding screenshots. Please ensure that no sensitive or irrelevant information is included and specify the expected outcome based on the provided data.

    We are following up to inquire whether you have found a resolution to the query you had posted. If you have identified a solution, we sincerely request you to share it with the community, as it may be beneficial to others encountering a similar issue.

     

    Thank you.


  • v-pnaroju-msft's avatar
    v-pnaroju-msft
    Icon for Community Support rankCommunity Support

    Hi Neiners,

    We kindly ask you to provide sample data that clearly represents your issue or question in a suitable format, without using screenshots. Please make sure that no sensitive or unrelated information is included. Also, mention the expected result based on the data you provide.

    We are also following up to check if you have found a solution to the query you posted. If you have, we sincerely request you to share it with the community, as it may help others facing a similar problem.


    Thank you.

     

  • v-pnaroju-msft's avatar
    v-pnaroju-msft
    Icon for Community Support rankCommunity Support

    Hi Neiners,

    We kindly request you to provide sample data that accurately represents your issue or query in an appropriate format, excluding screenshots. Please ensure that no sensitive or irrelevant information is included and specify the expected outcome based on the provided data.

    We are following up to inquire whether you have found a resolution to the query you had posted. If you have identified a solution, we sincerely request you to share it with the community, as it may be beneficial to others encountering a similar issue.


    Thank you.

  • v-pnaroju-msft's avatar
    v-pnaroju-msft
    Icon for Community Support rankCommunity Support

    Hi Neiners,

    Can you please confirm whether you have resolved the issue. If yes, you are welcome to share your workaround and mark it as a solution so that other users can benefit as well. This will be helpful for other community members who have similar problems to solve it faster.
    If we don’t hear back, we’ll go ahead and close this thread. Should you need further assistance in the future, we encourage you to reach out via the Microsoft Fabric Community Forum and create a new thread. We’ll be happy to help.

    Thank you.

  • v-pnaroju-msft's avatar
    v-pnaroju-msft
    Icon for Community Support rankCommunity Support

    Hi Neiners,

    Kindly confirm if your issue has been resolved. If yes, please feel free to share your workaround and mark it as the solution so that other users can benefit from it. This will help community members with similar problems find solutions more quickly.

    If we do not receive a response from you, we will proceed to close this thread. For any further assistance in the future, you are welcome to reach out through the Microsoft Fabric Community Forum by creating a new thread. We will be happy to assist you.

    Thank you.