<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: SQL Query SM in notebook in Data Science</title>
    <link>https://community.fabric.microsoft.com/t5/Data-Science/SQL-Query-SM-in-notebook/m-p/4807121#M853</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="754136" data-lia-user-login="Peter_23" class="lia-mention lia-mention-user"&gt;Peter_23&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;I wanted to follow up and confirm whether you’ve had the opportunity to review the information we provided. If you have any questions or need further clarification, please don’t hesitate to reach out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We appreciate your engagement and thank you for being an active part of the community.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Best regards,&lt;BR /&gt;Lakshmi.&lt;/STRONG&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 25 Aug 2025 16:22:43 GMT</pubDate>
    <dc:creator>v-lgarikapat</dc:creator>
    <dc:date>2025-08-25T16:22:43Z</dc:date>
    <item>
      <title>SQL Query SM in notebook</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Science/SQL-Query-SM-in-notebook/m-p/4804482#M850</link>
      <description>&lt;P&gt;hi comunnity, i n this time, I try to write a query in SQL to SM by notebook, I know the "explore" option to get the matrix data, and "write DAX queries" are useful tools to query data, but my user it's more comfortable with SQL syntax.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I decided to use notebook to query data from SM using SQL syntax. (sempy ) &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The first question is: it its possible to query as example: ? to SM &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;%%sql
SELECT YEAR(OrderDate) AS OrderYear,
       SUM((UnitPrice * Quantity) + Tax) AS GrossRevenue
FROM salesorders
GROUP BY YEAR(OrderDate)
ORDER BY OrderYear;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The next step, I'm tryting to use the library, but dont work . &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The SM is a power bi dataset, so I'm using the sempy library. I have a test.&lt;/P&gt;&lt;P&gt;In the notebook. I have the next code.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;import sempy.fabric as fabric

dataset = "SM-example matrix filtered by column" # Enter the name or ID of the semantic model
workspace = "TEst" # Enter the workspace name or ID in which the semantic model exists&amp;lt;p&amp;gt; &amp;lt;li-code lang="markup"&amp;gt;fabric.list_measures(dataset)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;so It's fine, but the next code, It trigger a expection.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;# convert to a spark dataframe so that can use select syntax 
sparkdf = spark.createDataFrame(fabricdf)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;ValueError&lt;SPAN&gt;: Some of types cannot be determined after inferring&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Why the valueerror ? &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;and the next code should be:&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;daxdf=sparkdf.select("Measure Name","Measure Expression").where(sparkdf["Measure Name"]=="Measure")

display(daxdf)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks in advance.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Aug 2025 05:05:42 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Science/SQL-Query-SM-in-notebook/m-p/4804482#M850</guid>
      <dc:creator>Peter_23</dc:creator>
      <dc:date>2025-08-22T05:05:42Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Query SM in notebook</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Science/SQL-Query-SM-in-notebook/m-p/4805283#M851</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="754136" data-lia-user-login="Peter_23" class="lia-mention lia-mention-user"&gt;Peter_23&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;Thanks for reaching out to the Microsoft fabric community forum&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can You Query SM with SQL Syntax?&lt;/P&gt;
&lt;P&gt;Not directly in the way you're hoping. Semantic models (SMs) in Power BI are fundamentally DAX-based, not SQL based. Even though sempy lets you interact with SMs in notebooks, it doesn't support SQL querying like:&lt;/P&gt;
&lt;P&gt;SELECT YEAR(OrderDate), SUM(...) FROM salesorders&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead, sempy is designed to:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;List metadata (tables, columns, measures)&lt;/LI&gt;
&lt;LI&gt;Execute DAX queries&lt;/LI&gt;
&lt;LI&gt;Retrieve results as pandas DataFrames&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;So your SQL Style query needs to be rewritten in DAX and executed via sempy's evaluate_dax() method.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Why the ValueError on spark.createDataFrame(fabricdf)?&lt;/P&gt;
&lt;P&gt;This error typically means that the fabricdf object you're passing to Spark doesn't have clearly inferrable types for all columns. Here's what might be going wrong:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;fabric.list_measures(dataset) returns a pandas DataFrame, not a Spark DataFrame.&lt;/LI&gt;
&lt;LI&gt;Spark needs explicit schema or cleanly inferrable types to convert a pandas DataFrame.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;If some columns contain mixed types (e.g., None, strings, numbers), Spark can't infer them automatically.&lt;/P&gt;
&lt;P&gt;How to Fix It&lt;/P&gt;
&lt;P&gt;Option 1: Stick with pandas&lt;/P&gt;
&lt;P&gt;If you're just filtering and displaying metadata, pandas is simpler and works fine:&lt;/P&gt;
&lt;P&gt;python&lt;/P&gt;
&lt;P&gt;import sempy.fabric as fabric&lt;/P&gt;
&lt;P&gt;dataset = "SM-example matrix filtered by column"&lt;/P&gt;
&lt;P&gt;workspace = "TEst"&lt;/P&gt;
&lt;P&gt;fabricdf = fabric.list_measures(dataset)&lt;/P&gt;
&lt;P&gt;Filter using pandas&lt;/P&gt;
&lt;P&gt;daxdf = fabricdf[fabricdf["Measure Name"] == "Measure"]&lt;/P&gt;
&lt;P&gt;display(daxdf)&lt;/P&gt;
&lt;P&gt;Option 2: Explicitly define schema for Spark&lt;/P&gt;
&lt;P&gt;If you &lt;EM&gt;must&lt;/EM&gt; use Spark:&lt;/P&gt;
&lt;P&gt;from pyspark.sql.types import StructType, StructField, StringType&lt;/P&gt;
&lt;P&gt;schema = StructType([&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; StructField("Measure Name", StringType(), True),&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; StructField("Measure Expression", StringType(), True)&lt;/P&gt;
&lt;P&gt;])&lt;/P&gt;
&lt;P&gt;sparkdf = spark.createDataFrame(fabricdf, schema=schema)&lt;/P&gt;
&lt;P&gt;daxdf = sparkdf.select("Measure Name", "Measure Expression").where(sparkdf["Measure Name"] == "Measure")&lt;/P&gt;
&lt;P&gt;display(daxdf)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://learn.microsoft.com/en-us/fabric/data-science/semantic-link-semantic-propagation" target="_blank" rel="noopener"&gt;Semantic link propagation with SemPy - Microsoft Fabric | Microsoft Learn&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://community.fabric.microsoft.com/t5/Data-Science-Community-Blog/SemPy-in-Microsoft-Fabric-From-SQL-Scripts-to-Semantic-Models/ba-p/4319858" target="_blank" rel="noopener"&gt;SemPy in Microsoft Fabric: From SQL Scripts to Sem... - Microsoft Fabric Community&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Best Regards,&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Lakshmi.&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Aug 2025 16:21:46 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Science/SQL-Query-SM-in-notebook/m-p/4805283#M851</guid>
      <dc:creator>v-lgarikapat</dc:creator>
      <dc:date>2025-08-25T16:21:46Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Query SM in notebook</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Science/SQL-Query-SM-in-notebook/m-p/4807121#M853</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="754136" data-lia-user-login="Peter_23" class="lia-mention lia-mention-user"&gt;Peter_23&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;I wanted to follow up and confirm whether you’ve had the opportunity to review the information we provided. If you have any questions or need further clarification, please don’t hesitate to reach out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We appreciate your engagement and thank you for being an active part of the community.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Best regards,&lt;BR /&gt;Lakshmi.&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Aug 2025 16:22:43 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Science/SQL-Query-SM-in-notebook/m-p/4807121#M853</guid>
      <dc:creator>v-lgarikapat</dc:creator>
      <dc:date>2025-08-25T16:22:43Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Query SM in notebook</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Science/SQL-Query-SM-in-notebook/m-p/4807450#M854</link>
      <description>&lt;P&gt;Thanks &lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="1271221" data-lia-user-login="v-lgarikapat" class="lia-mention lia-mention-user"&gt;v-lgarikapat&lt;/a&gt;&amp;nbsp; I understand&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Aug 2025 02:19:33 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Science/SQL-Query-SM-in-notebook/m-p/4807450#M854</guid>
      <dc:creator>Peter_23</dc:creator>
      <dc:date>2025-08-26T02:19:33Z</dc:date>
    </item>
  </channel>
</rss>

