<?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 Convert SQL query to DAX in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Convert-SQL-query-to-DAX/m-p/4130696#M164069</link>
    <description>&lt;P&gt;Hi community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;can someone pls help me out with this sql query to be converted in dax?&lt;/P&gt;&lt;P&gt;All data is in the same table. The sql speaks for itself but goal is to aggregate specific positions by id and the booking_date of revenue and get corresponding costs.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for any input on this.&lt;/P&gt;&lt;P&gt;best regards,&lt;/P&gt;&lt;P&gt;sinan&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;with rev as&lt;BR /&gt;(&lt;BR /&gt;select&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; id,&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; max(case when pos in ('A', 'B') then BOOKING_DATE end) as booking_date,&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sum(book_amount) as revenue&lt;BR /&gt;from&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; table&lt;BR /&gt;where&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cat = 'NEW' and&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; pos in ('A','B','C','D','E','F')&lt;BR /&gt;group by id&lt;BR /&gt;),&lt;/P&gt;&lt;P&gt;cost as&lt;BR /&gt;(&lt;BR /&gt;select&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; id&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sum(book_amount) as cost&lt;BR /&gt;from&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; table&lt;BR /&gt;where&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cat = 'NEW' and&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; pos in ('X')&lt;BR /&gt;group by id&lt;BR /&gt;)&lt;/P&gt;&lt;P&gt;select&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a.id&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a.booking_date,&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a.revenue,&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; b.cost&lt;BR /&gt;from&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; rev a inner join&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cost b on a.id = b.id&lt;BR /&gt;;&lt;/P&gt;</description>
    <pubDate>Tue, 03 Sep 2024 07:41:40 GMT</pubDate>
    <dc:creator>SG2015</dc:creator>
    <dc:date>2024-09-03T07:41:40Z</dc:date>
    <item>
      <title>Convert SQL query to DAX</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Convert-SQL-query-to-DAX/m-p/4130696#M164069</link>
      <description>&lt;P&gt;Hi community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;can someone pls help me out with this sql query to be converted in dax?&lt;/P&gt;&lt;P&gt;All data is in the same table. The sql speaks for itself but goal is to aggregate specific positions by id and the booking_date of revenue and get corresponding costs.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for any input on this.&lt;/P&gt;&lt;P&gt;best regards,&lt;/P&gt;&lt;P&gt;sinan&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;with rev as&lt;BR /&gt;(&lt;BR /&gt;select&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; id,&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; max(case when pos in ('A', 'B') then BOOKING_DATE end) as booking_date,&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sum(book_amount) as revenue&lt;BR /&gt;from&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; table&lt;BR /&gt;where&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cat = 'NEW' and&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; pos in ('A','B','C','D','E','F')&lt;BR /&gt;group by id&lt;BR /&gt;),&lt;/P&gt;&lt;P&gt;cost as&lt;BR /&gt;(&lt;BR /&gt;select&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; id&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sum(book_amount) as cost&lt;BR /&gt;from&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; table&lt;BR /&gt;where&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cat = 'NEW' and&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; pos in ('X')&lt;BR /&gt;group by id&lt;BR /&gt;)&lt;/P&gt;&lt;P&gt;select&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a.id&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a.booking_date,&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a.revenue,&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; b.cost&lt;BR /&gt;from&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; rev a inner join&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cost b on a.id = b.id&lt;BR /&gt;;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Sep 2024 07:41:40 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Convert-SQL-query-to-DAX/m-p/4130696#M164069</guid>
      <dc:creator>SG2015</dc:creator>
      <dc:date>2024-09-03T07:41:40Z</dc:date>
    </item>
    <item>
      <title>Re: Convert SQL query to DAX</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Convert-SQL-query-to-DAX/m-p/4131067#M164079</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="401930" data-lia-user-login="SG2015" class="lia-mention lia-mention-user"&gt;SG2015&lt;/a&gt;&amp;nbsp;- You only need to create measures for the two calculations, the visual you build will do the rest, the DAX for these measures is below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Revenue =

CALCULATE (
    Table[book_amount],
    KEEPFILTERS ( Table[cat] = "NEW" ),
    KEEPFILTERS ( Table[pos] IN { "A", "B", "C", "D", "E", "F" } )
)

Cost = 

CALCULATE (
    Table[book_amount],
    KEEPFILTERS ( Table[cat] = "NEW" ),
    KEEPFILTERS ( Table[pos] = "X" )
)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Create these in two different measures and place them against the appropriate columns from your table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this works, please accept as a solution - it helps with visibility for others with the same challenge.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Sep 2024 11:05:36 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Convert-SQL-query-to-DAX/m-p/4131067#M164079</guid>
      <dc:creator>mark_endicott</dc:creator>
      <dc:date>2024-09-03T11:05:36Z</dc:date>
    </item>
    <item>
      <title>Re: Convert SQL query to DAX</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Convert-SQL-query-to-DAX/m-p/4132004#M164134</link>
      <description>&lt;P&gt;Thank you Mark.&lt;/P&gt;&lt;P&gt;Good idea with the measures, but I am afraid, this is not the output I need.&lt;/P&gt;&lt;P&gt;I'm rather looking for a table-output than a measure-output. Something like the following, although I still couldn't figure out the cost part (red color):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;EVALUATE&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;SUMMARIZ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OUNT]&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;,&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;FONT color="#FF0000"&gt;&lt;SPAN&gt;"Cost"&lt;/SPAN&gt; , &lt;SPAN&gt;CALCULATE&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;sum&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;table[BOOK_AMOUNT]&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;, &lt;SPAN&gt;FILTER&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;table, &lt;SPAN&gt;table[cat]&lt;/SPAN&gt; = &lt;SPAN&gt;"NEW"&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;, &lt;SPAN&gt;FILTER&lt;/SPAN&gt;&lt;SPAN&gt;(table, table&lt;/SPAN&gt;&lt;SPAN&gt;[POS]&lt;/SPAN&gt; = &lt;SPAN&gt;"X"&lt;/SPAN&gt;&lt;/FONT&gt;&lt;SPAN&gt;&lt;FONT color="#FF0000"&gt;))&lt;/FONT&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code in red is just for demonstration. I know that calculate and filter does not work in this context. But how would I make it work so it filters independently from revenue-data? Just like in the SQL? Can I use two summarizecolumns-functions - one for revenue and one for cost - and then join/merge them together? Is the function &lt;SPAN&gt;SUMMARIZECOLUMNS the right one or is it better done with CALCULATETABLE? Am really struggling with the DAX-Syntax for problems like this.&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I basically want to create a table on the fly. The underlying data is huge and I don't want to create an aggregated table with power query. The fields ID and BOOKING_DATE will be joined with dimensions afterwards.&lt;/P&gt;&lt;P&gt;Main Purpose:&lt;/P&gt;&lt;P&gt;Cost data has booking_dates which are different from booking_dates of revenue data. With the summarized table I want 1.) an aggreagtion of the huge underlying data and 2.) booking_date to behave as if costs have been realized on same date as revenue, to use only one date slicer afterwards. Output should be like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;ID&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;&amp;nbsp;BOOKING_DATE&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;&amp;nbsp;REVENUE&amp;nbsp;&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;&amp;nbsp;COST&amp;nbsp;&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2024-01-01&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2024-01-02&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;2024-01-03&lt;/TD&gt;&lt;TD&gt;30&lt;/TD&gt;&lt;TD&gt;25&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;2024-01-04&lt;/TD&gt;&lt;TD&gt;40&lt;/TD&gt;&lt;TD&gt;45&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;2024-01-05&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks. I appreciate your help.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best. Sinan&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Sep 2024 22:59:59 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Convert-SQL-query-to-DAX/m-p/4132004#M164134</guid>
      <dc:creator>SG2015</dc:creator>
      <dc:date>2024-09-03T22:59:59Z</dc:date>
    </item>
    <item>
      <title>Re: Convert SQL query to DAX</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Convert-SQL-query-to-DAX/m-p/4132954#M164150</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="401930" data-lia-user-login="SG2015" class="lia-mention lia-mention-user"&gt;SG2015&lt;/a&gt;&amp;nbsp;- I'm not sure you understand how calculated tables work. They are not computed "on the fly" as people interact with the data in a report. They are calculated at the point of a refresh, but after all of the transformations are complete in Power Query. The means they are less efficient for the data model and not subjected to the same compression algorithms that tables created in Power Query are, you would actually be better off creating an aggregated table in Power Query.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, if you would like to make this as a calculated table, here is the DAX:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;CALCULATETABLE (
    SUMMARIZE (
        'Table',
        'Table'[ID],
        'Table'[BOOKING_DATE],
        "Revenue",
            CALCULATE (
                SUM ( 'Table'[BOOK_AMOUNT] ),
                KEEPFILTERS ( 'Table'[POS] IN { "A", "B", "C", "D", "E", "F" } )
            ),
        "Cost", CALCULATE ( SUM ( 'Table'[BOOK_AMOUNT] ), KEEPFILTERS ( 'Table'[POS] = "X" ) )
    ),
    'Table'[Column1] = "NEW"
)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this works, please mark it as the solution for the visibility of others.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Sep 2024 08:55:53 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Convert-SQL-query-to-DAX/m-p/4132954#M164150</guid>
      <dc:creator>mark_endicott</dc:creator>
      <dc:date>2024-09-04T08:55:53Z</dc:date>
    </item>
    <item>
      <title>Re: Convert SQL query to DAX</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Convert-SQL-query-to-DAX/m-p/4139983#M164500</link>
      <description>&lt;P&gt;Thank you Mark. I will give it another thought.&lt;/P&gt;&lt;P&gt;But for now, this is exactly what I would need. Thank you very much.&lt;/P&gt;&lt;P&gt;Best, Simon&lt;/P&gt;</description>
      <pubDate>Mon, 09 Sep 2024 06:41:57 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Convert-SQL-query-to-DAX/m-p/4139983#M164500</guid>
      <dc:creator>SG2015</dc:creator>
      <dc:date>2024-09-09T06:41:57Z</dc:date>
    </item>
  </channel>
</rss>

