Don't miss your chance to take the Fabric Data Engineer (DP-600) exam for FREE! Find out how by attending the DP-600 session on April 23rd (pacific time), live or on-demand.
Learn moreNext up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now
Your file has been submitted successfully. We’re processing it now - please check back in a few minutes to view your report.
05-02-2020 08:18 AM - last edited 05-02-2020 08:19 AM
In my recent quest to create or catalog as many DAX equivalents for Excel functions, with DEC2BIN and BIN2DEC figured out, we can implement bit shifting functions such as BITRSHIFT (Bit Right Shift):
BITLSHIFT =
VAR __DecimalNumber = SELECTEDVALUE('Decimal'[Decimal],0)
VAR __Bits = SELECTEDVALUE('Bits'[Bits],0)
VAR __BitShift = REPT("0",ABS(__Bits))
VAR __Binary =
IF(
__DecimalNumber = 0,
"0",
VAR __Table =
ADDCOLUMNS(
GENERATESERIES(0,LOG(__DecimalNumber,2),1),
"Bit",MOD(TRUNC(__DecimalNumber / POWER(2,[Value])),2)
)
RETURN
CONCATENATEX(__Table,[Bit],"",[Value],DESC)
)
VAR __Shifted =
IF(
__Bits >= 0,
LEFT(__Binary,LEN(__Binary) - __Bits),
__Binary & __BitShift,
)
VAR __Table =
ADDCOLUMNS(
GENERATESERIES(0,LEN(__Shifted)-1,1),
"Decimal",POWER(2,[Value]) * MID(__Shifted,LEN(__Shifted) - [Value],1)
)
RETURN
SUMX(__Table,[Decimal])
NOTE: Does it support negative numbers you ask? Yes and no. It does not support negative input decimal numbers to shift but then neither does the Excel BITRSHIFT function. However, you can have a negative number of bits to shift just like the Excel version. Using a negative number of bits effectively makes this a BITLSHIFT.
eyJrIjoiYzkwMzQxYjAtNjZhMC00OGJmLWFjNjUtYWQxNjlmNjM5MmUxIiwidCI6IjRhMDQyNzQzLTM3M2EtNDNkMi04MjdiLTAwM2Y0YzdiYTFlNSIsImMiOjN9