简体   繁体   中英

Convert SQL to Power BI

I'm very new to Power BI, therefore please be indulgent with me :D

Actually, I have 2 tables called Report and Mails. In the Report table I have a column Racine and in the Mails table I have a column called Body that might contain Racine.

I would like to convert this sql to Power BI

select Report.Racine, Mails.Body 
from Report, Mails
where Mails.Body LIKE '%' + Report.UserId + '%'; 

Example:

Table Report

Racine
1234
5678
9012
0987

Table Mails

Body
'abc 1234 AZER'
'PO5678 tgcv'
'YF1234GHQ'

The result should be

Racine             Body
1234               'abc 1234 AZER'
1234               'YF1234GHQ'
5678               'PO5678 tgcv'
9012               *null*
0987               *null*

Is it possible?

Thx in advance.

If you have a situation where these tables come from different datasource, you can calculate a new table via DAX (Data -> New Table):

VAR __tmp1 =
    FILTER (
        SUMMARIZECOLUMNS (
            Report[Racine],
            Mails[Body],
            "condition",
                CONTAINSSTRING (
                    SELECTEDVALUE ( Mails[Body] ),
                    SELECTEDVALUE ( Report[Racine] )
                )
        ),
        [condition]
    )
VAR __tmp2 =
    ADDCOLUMNS (
        CALCULATETABLE (
            Report,
            NOT ( Report[Racine] IN ( CALCULATETABLE ( Report, __tmp1 ) ) )
        ),
        "Body", BLANK (),
        "condition", FALSE
    )
RETURN
    UNION ( __tmp1, __tmp2 )

在此处输入图片说明

There are two options.

  1. You can import your exact query as per the image below.

    在此处输入图片说明

  2. You import both source tables in PowerBI. You will need to write a query in PowerQuery (M) in PowerBI to extract the numeric Racine from the body. You can then merge the data in another M Query or join in your model. If using the latter you will need to "show items with no data" on the visuals so that blanks/nulls appear.

在此处输入图片说明

You can find and example of the M code you need to write here . Pasted below for convenience.

#"Added Custom" = Table.AddColumn(#"Promoted Headers", "Custom", each Text.Combine(List.RemoveNulls(List.Transform(Text.ToList([Name]),each if Value.Is(Value.FromText(_), type number) then _ else null))))
 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM