<?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 How to reference a column from a virtual table to be used in a IF statement ? in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-reference-a-column-from-a-virtual-table-to-be-used-in-a/m-p/3219619#M117642</link>
    <description>&lt;P&gt;Hello Power BI &amp;amp; DAX community,&lt;BR /&gt;&lt;BR /&gt;I am trying to create a measure that calculates the number of new suppliers for each period based on if the suppliers registration date is within that period or not. For example if a supplier has Registration date 20220120 it should be counted as a new supplier for period 202201 etc.&lt;BR /&gt;&lt;BR /&gt;This is what I have tried:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;EVALUATE 
VAR cte_Periods =
    SUMMARIZECOLUMNS (
        Dim_Time[Period],
        "Monthmin", DATE ( YEAR ( EOMONTH ( MAX (Dim_Time[Date]), 0 ) ), MONTH ( EOMONTH ( MAX (Dim_Time[Date]), 0 ) ), 1 ),
        "Monthmax", MAX (Dim_Time[Date] )
    )
RETURN
    SUMX (
        SUMMARIZECOLUMNS (
            Purchase[Supplier number],
            "Reg date", MIN(Purchase[Registration date])
        ),
        IF (
            AND (
                [Reg date] &amp;gt;= SELECTEDVALUE ( cte_Periods[Monthmin] ),
                [Reg date] &amp;lt;= SELECTEDVALUE ( cte_Periods[Monthmax] )
            ),
            1,
            BLANK ()
        )
    )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But complains that it cannot find table 'cte_Periods'. How can I apply the virtual cte_Periods table to be used in the return statement ?. I would assume that I need somekind of cross apply or cross join ?&lt;BR /&gt;&lt;BR /&gt;I solved it like this in SQL:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;WITH cte_Periods
AS (SELECT TIM.Period, 
           DATEFROMPARTS(YEAR(EOMONTH(MAX(TIM.Date))), MONTH(EOMONTH(MAX(TIM.Date))), 1) AS Monthmin,
           CONVERT(DATE, MAX(TIM.Date)) AS Monthmax
    FROM [dbo].[Dim_Time] AS TIM
    GROUP BY TIM.Period)
SELECT COUNT(DISTINCT CASE
                    WHEN CONVERT(DATE, CONVERT(VARCHAR(8), F.[Registration date]))
                         BETWEEN p.Monthmin AND p.Monthmax THEN
                        F.[Supplier number]
                    ELSE
                        NULL
                END
            ) AS [Number of New Suppliers],
       p.Period
FROM [dbo].[Purchase] AS F
    CROSS JOIN cte_Periods AS p
WHERE F.[Purchase date] &amp;gt;= p.Monthmin
      AND F.[Purchase date] &amp;lt;= p.Monthmax
GROUP BY p.Period;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;BR /&gt;Best regards,&lt;BR /&gt;Rubrix&lt;/P&gt;</description>
    <pubDate>Thu, 04 May 2023 12:13:50 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2023-05-04T12:13:50Z</dc:date>
    <item>
      <title>How to reference a column from a virtual table to be used in a IF statement ?</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-reference-a-column-from-a-virtual-table-to-be-used-in-a/m-p/3219619#M117642</link>
      <description>&lt;P&gt;Hello Power BI &amp;amp; DAX community,&lt;BR /&gt;&lt;BR /&gt;I am trying to create a measure that calculates the number of new suppliers for each period based on if the suppliers registration date is within that period or not. For example if a supplier has Registration date 20220120 it should be counted as a new supplier for period 202201 etc.&lt;BR /&gt;&lt;BR /&gt;This is what I have tried:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;EVALUATE 
VAR cte_Periods =
    SUMMARIZECOLUMNS (
        Dim_Time[Period],
        "Monthmin", DATE ( YEAR ( EOMONTH ( MAX (Dim_Time[Date]), 0 ) ), MONTH ( EOMONTH ( MAX (Dim_Time[Date]), 0 ) ), 1 ),
        "Monthmax", MAX (Dim_Time[Date] )
    )
RETURN
    SUMX (
        SUMMARIZECOLUMNS (
            Purchase[Supplier number],
            "Reg date", MIN(Purchase[Registration date])
        ),
        IF (
            AND (
                [Reg date] &amp;gt;= SELECTEDVALUE ( cte_Periods[Monthmin] ),
                [Reg date] &amp;lt;= SELECTEDVALUE ( cte_Periods[Monthmax] )
            ),
            1,
            BLANK ()
        )
    )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But complains that it cannot find table 'cte_Periods'. How can I apply the virtual cte_Periods table to be used in the return statement ?. I would assume that I need somekind of cross apply or cross join ?&lt;BR /&gt;&lt;BR /&gt;I solved it like this in SQL:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;WITH cte_Periods
AS (SELECT TIM.Period, 
           DATEFROMPARTS(YEAR(EOMONTH(MAX(TIM.Date))), MONTH(EOMONTH(MAX(TIM.Date))), 1) AS Monthmin,
           CONVERT(DATE, MAX(TIM.Date)) AS Monthmax
    FROM [dbo].[Dim_Time] AS TIM
    GROUP BY TIM.Period)
SELECT COUNT(DISTINCT CASE
                    WHEN CONVERT(DATE, CONVERT(VARCHAR(8), F.[Registration date]))
                         BETWEEN p.Monthmin AND p.Monthmax THEN
                        F.[Supplier number]
                    ELSE
                        NULL
                END
            ) AS [Number of New Suppliers],
       p.Period
FROM [dbo].[Purchase] AS F
    CROSS JOIN cte_Periods AS p
WHERE F.[Purchase date] &amp;gt;= p.Monthmin
      AND F.[Purchase date] &amp;lt;= p.Monthmax
GROUP BY p.Period;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;BR /&gt;Best regards,&lt;BR /&gt;Rubrix&lt;/P&gt;</description>
      <pubDate>Thu, 04 May 2023 12:13:50 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-reference-a-column-from-a-virtual-table-to-be-used-in-a/m-p/3219619#M117642</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2023-05-04T12:13:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to reference a column from a virtual table to be used in a IF statement ?</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-reference-a-column-from-a-virtual-table-to-be-used-in-a/m-p/3223383#M117949</link>
      <description>&lt;P&gt;Virtual tables don't have a selected value. Use an aggregation that makes sense for your scenario, like MINX(cte_periods,[Monthmin]).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 06 May 2023 22:01:46 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-reference-a-column-from-a-virtual-table-to-be-used-in-a/m-p/3223383#M117949</guid>
      <dc:creator>lbendlin</dc:creator>
      <dc:date>2023-05-06T22:01:46Z</dc:date>
    </item>
  </channel>
</rss>

