Forum Discussion

UmeshGoti's avatar
UmeshGoti
Helper I
1 year ago
Solved

Need to fix issue in Create Semantic Layer by providing sql query

Hello community,   Currently i am creating semantic layer based on SQL query and database details i have. for this i am using below API  POST : https://api.fabric.microsoft.com/v1/workspaces/{wor...
  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi, UmeshGoti 

    Based on your description, I made a working case.
    First, I created a basic semantic model in Power BI desktop:

    Save it as a PBIP file:

    Use the following template to make a payload:

    SemanticModel definition - Microsoft Fabric REST APIs | Microsoft Learn

    The format should look like this:

    {
      "displayName": "SemanticModel 1",
      "description": "A semantic model description.",
      "definition": {
        "parts": [....]
      }
    }
    
    

    Next, let's process the Parts section one by one:

    According to the section in the part, the content of each file in the folder is encrypted:

    definition/database.tmdl
    definition/model.tmdl
    definition/tables/Table1.tmdl
    definition/model.tmdl
    definition/tables/Table.tmdl
    definition.pbism
    diagramLayout.json
    .platform

    Here I'm using Python to send requests. My script is as follows:

    I'm using service principal authentication:

    import requests
    from azure.identity import ClientSecretCredential
    
    class MyAPI:
        def __init__(self,client_id,client_secret,tenant_id):
            self.scope = 'https://analysis.windows.net/powerbi/api/.default'
            self.grant_type = 'client_credentials'
            self.client_id = client_id
            self.client_secret = client_secret
            self.tenant_id = tenant_id
        def auth12 (self):
            cred = ClientSecretCredential(
                authority = "https://login.microsoftonline.com/",
                tenant_id=self.tenant_id,
                client_id=self.client_id,
                client_secret=self.client_secret)
            acc_token = cred.get_token(self.scope)
            return acc_token.token
        
    api = MyAPI(
        client_id = 'xxxx',
        client_secret = 'xxxx',
        tenant_id = 'xxx'
    )
    
    workspaceId = 'xxxx'
    
    urlPost = f"https://api.fabric.microsoft.com/v1/workspaces/{workspaceId}/semanticModels"
    bodyPost = {
      "displayName": "SemanticModel 1",
      "description": "A semantic model description.",
      "definition": {
        "parts": [
            {
                "path": "definition/database.tmdl",
                "payload": "xxxx",
                "payloadType": "InlineBase64"
            },
            {
                "path": "definition/model.tmdl",
                "payload": "xxxx",
                "payloadType": "InlineBase64"
            },
            {
                "path": "definition/tables/Table.tmdl",
                "payload": "xxxxx",
                "payloadType": "InlineBase64"
            },
            {
                "path": "definition.pbism",
                "payload": "xxxx",
                "payloadType": "InlineBase64"
            },
            {
                "path": "diagramLayout.json",
                "payload": "xxxxx",
                "payloadType": "InlineBase64"
            },
            {
                "path": ".platform",
                "payload": "xxxxx",
                "payloadType": "InlineBase64"
            }
        ]
      }
    }
    token = api.auth12()
    headers = {
        'Authorization': f'Bearer {token}',
        'Content-Type': 'application/json'
    }
    
    res = requests.post(url=urlPost,headers=headers,json=bodyPost)
    
    print(res.status_code)

    It gets the right repercussions code and sets up the right semantic model on Power BI Service:

     

     

    Best Regards

    Jianpeng Li

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.