PowerBI/DAX calculated column to compute network CIDR from a specifc machine/network ip and mask

1

I have only the "basic" PBI installed on my company server, so I need to find a solution with DAX or PowerQuery. No Python, No R ... :/

I have two tables with Networks ip and mask, and another one with Machines ip and mask.

I would like to set a relation between the two tables.

My idea is to compute a new column in each table. It will be the network ip and mask in CIDR notation, which I will use as a common key to set up the relation:

Machines --N:1--> Networks

Networks Table
==
netip,        netmask          -> netcidr *
10.10.1.32,   255.255.255.224  -> 10.10.1.32/27


Machines Table
==
machineip,    netmask          -> netcidr *
10.10.1.35,   255.255.255.224  -> 10.10.1.32/27
10.10.1.38,   255.255.255.224  -> 10.10.1.32/27
  • I need to compute the netcidr columns in both case.

I have done it with Python function, but I need to convert this in DAX or PowerQuery or SQL:

def network2(ipmask):
    ip, cidr = ipmask.split('/')
    print(">", ip, cidr)
    a, b, c, d = ip.split('.')
    ipn = (((int(a)*256+int(b))*256)+int(c))*256+int(d)

    cidr = int(cidr)
    mask = (0xffffffff >> (32 - cidr)) << (32 - cidr)
    net = ipn & mask

    print (ipn, cidr, mask, net )
    
    a = net%(256**4)//(256**3)
    b = net%(256**3)//(256**2)
    c = net%(256**2)//(256**1)
    d = net%(256**1)//(256**0)
    
    #net IP decimal
    print(">>>old  - ip: ", ipmask)
    print(">>>nets - ip: ", a, b, c, d, "/", cidr)


    #ip, cidr, mask
    # for i in ip.split('.')

network2('10.10.1.32/27') #mask 255.255.255.224
network2('10.10.1.38/27')


>>>old  - ip:  10.10.1.32/27
>>>nets - ip:  10 10 1 32 / 27
>>>old  - ip:  10.10.1.38/27
>>>nets - ip:  10 10 1 32 / 27

It works in Python and now I try to do it in DAX:

cidr_net = 
// ...
VAR mask = data[mask_lan]
VAR dot1 = FIND(".", mask, 1, 0)
VAR p1   = VALUE(IF(dot1>0, (MID(mask, 1, dot1-1)), "0"))
VAR dot2 = FIND(".", mask, dot1+1, 0)
VAR p2   = VALUE(IF(dot2>0, MID(mask, dot1+1, dot2-1-dot1), "0"))
VAR dot3 = FIND(".", mask, dot2+1, 0)
VAR p3   = VALUE(IF(dot3>0, MID(mask, dot2+1, dot3-1-dot2), "0"))
VAR p4   = VALUE(IF(dot3>0, MID(mask, dot3+1, len(mask)-dot3), "0"))

VAR ip    = data[ip_lan]
VAR d1    = FIND(".", ip, 1, 0)
VAR ip1   = VALUE(IF(d1>0, (MID(ip, 1, d1-1)), "0"))
VAR d2    = FIND(".", ip, d1+1, 0)
VAR ip2   = VALUE(IF(d2>0, MID(ip, d1+1, d2-1-d1), "0"))
VAR d3    = FIND(".", ip, d2+1, 0)
VAR ip3   = VALUE(IF(d3>0, MID(ip, d2+1, d3-1-d2), "0"))
VAR ip4   = VALUE(IF(d3>0, MID(ip, d3+1, len(ip)-d3), "0"))

VAR n1    = p1 && ip1
VAR n2    = p2 && ip2
VAR n3    = p3 && ip3
VAR n4    = p4 && ip4


VAR cidr = FORMAT( 32-log( 4294967296-((((p1*256+p2)*256)+p3)*256+p4), 2), "##")
RETURN CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(n1, "."), n2), "."), n3), "."), n4), "/"), cidr)

But it does not work:

1/ I cannot find bitwise operators in DAX ... so "VAR n1 = p1 && ip1" does not work as a bitwise and. Is it possible to do the bitwise and another way ?

2/ Is there another way to do this more easily in DAX ?

3/ Otherwise maybee with PowerQuery ?

networking
powerbi
ip
dax
cidr
asked on Stack Overflow Feb 17, 2021 by Malo • edited Feb 18, 2021 by Malo

2 Answers

1

A DAX solution can be implemented, even if it would be better to move this computation to SQL or Power Query

I think I fixed the final steps of your code

[cidr_net] =
        // il faut un biwise and de l ip et du mask pour trouver le cidre du reseau ...
        VAR mask = "255.255.255.224"
        VAR dot1 =
            FIND ( ".", mask, 1, 0 )
        VAR p1 =
            VALUE ( IF ( dot1 > 0, ( MID ( mask, 1, dot1 - 1 ) ), "0" ) )
        VAR dot2 =
            FIND ( ".", mask, dot1 + 1, 0 )
        VAR p2 =
            VALUE ( IF ( dot2 > 0, MID ( mask, dot1 + 1, dot2 - 1 - dot1 ), "0" ) )
        VAR dot3 =
            FIND ( ".", mask, dot2 + 1, 0 )
        VAR p3 =
            VALUE ( IF ( dot3 > 0, MID ( mask, dot2 + 1, dot3 - 1 - dot2 ), "0" ) )
        VAR p4 =
            VALUE ( IF ( dot3 > 0, MID ( mask, dot3 + 1, LEN ( mask ) - dot3 ), "0" ) )
        VAR ip = "10.10.1.38"
        VAR d1 =
            FIND ( ".", ip, 1, 0 )
        VAR ip1 =
            VALUE ( IF ( d1 > 0, ( MID ( ip, 1, d1 - 1 ) ), "0" ) )
        VAR d2 =
            FIND ( ".", ip, d1 + 1, 0 )
        VAR ip2 =
            VALUE ( IF ( d2 > 0, MID ( ip, d1 + 1, d2 - 1 - d1 ), "0" ) )
        VAR d3 =
            FIND ( ".", ip, d2 + 1, 0 )
        VAR ip3 =
            VALUE ( IF ( d3 > 0, MID ( ip, d2 + 1, d3 - 1 - d2 ), "0" ) )
        VAR ip4 =
            VALUE ( IF ( d3 > 0, MID ( ip, d3 + 1, LEN ( ip ) - d3 ), "0" ) )
        VAR ipAddress = ( ( ip1 * 256 + ip2 ) * 256 + ip3 ) * 256 + ip4
        VAR ipMask = ( ( p1 * 256 + p2 ) * 256 + p3 ) * 256 + p4
        VAR unmask = 2 ^ 32 - ipMask
        VAR ipNet =
            INT ( ipAddress / unmask ) * unmask
        VAR in1 =
            INT ( ipNet / 2 ^ 24 )
        VAR in2 =
            INT ( MOD ( ipNet, 2 ^ 24 ) / 2 ^ 16 )
        VAR in3 =
            INT ( MOD ( ipNet, 2 ^ 16 ) / 2 ^ 8 )
        VAR in4 =
            MOD ( ipNet, 2 ^ 8 )
        VAR Result =
            in1 & "." & in2 & "." & in3 & "." & in4 & "/"
                & 32 - LOG ( unmask, 2 )
        RETURN
            Result

this is a link to a working version of this code on DAX.do

answered on Stack Overflow Feb 18, 2021 by sergiom
1

I implemented a T-SQL version of the conversion. It uses the STRING_SPLIT() function, that's available since SQL Server 2016. It's very complicated and it also might be bad from the performance point of view. I wouldn't use it in production, but just to get the idea here it is

first I create a sample table

create table t( ip varchar(40) not null, mask varchar(40) not null);
insert into t(ip, mask) values ('10.0.1.38', '255.255.255.224');

then the query

WITH CTE AS (
SELECT ip, mask, B.ipn, M.masknc
from t AS A 
CROSS APPLY ( 
SELECT (([1] * 256 + [2]) * 256 + [3]) * 256 + [4] AS ipn FROM
(SELECT CAST([value] AS BIGINT) AS value, ID = ROW_NUMBER() OVER(ORDER BY(SELECT NULL)) 
FROM STRING_SPLIT(A.ip, '.') ) AS src
PIVOT
(MAX(value) FOR ID in ([1],[2],[3],[4]) ) AS P ) AS B
CROSS APPLY ( 
SELECT CAST(256 AS BIGINT) * 256 * 256 * 256 - ((([1] * 256 + [2]) * 256 + [3]) * 256 + [4]) AS masknc FROM
(SELECT CAST([value] AS BIGINT) AS value, ID = ROW_NUMBER() OVER(ORDER BY(SELECT NULL)) 
FROM STRING_SPLIT(A.mask, '.') ) AS src
PIVOT
(MAX(value) FOR ID in ([1],[2],[3],[4]) ) AS P ) AS M
), CTE1 AS (
SELECT C.ip, C.mask, CAST(C.ipn / C.masknc AS BIGINT) * C.masknc AS netipn, 
32 - LOG(C.masknc, 2) AS maskc, 
E32 = CAST(256 AS BIGINT) * 256 * 256 * 256, 
E24 = CAST(256 AS BIGINT) * 256 * 256, 
E16 = CAST(256 AS BIGINT) * 256,
E8 = CAST(256 AS BIGINT)
FROM CTE AS C
), CTE2 AS (
SELECT C.ip, C.mask, netip1 = FLOOR(netipn / E24), netip2 = FLOOR((netipn % E24)/ E16),
netip3 = FLOOR((netipn % E16) / E8), netip4 = FLOOR(netipn % E8), C.maskc
FROM CTE1 AS C
)
SELECT C.ip, C.mask, CAST(C.netip1 AS VARCHAR(3)) + '.' + CAST(C.netip2 AS VARCHAR(3)) + '.' +
CAST(C.netip3 AS VARCHAR(3)) + '.' + CAST(C.netip4 AS VARCHAR(3)) + '/' + CAST(C.maskc AS VARCHAR(4)) AS netip  
FROM CTE2 AS C

Here is the link of this code on dbfiddle.uk

I think the best option might be to use M in Power Query.

answered on Stack Overflow Feb 19, 2021 by sergiom

User contributions licensed under CC BY-SA 3.0