Skip to main content

I'm trying to replace some custom SQL with a Tableau prep flow

 

I'm trying to move some custom SQL into a Tableau Prep flow to replace the dashboard data sources and make it easier for non coders to use in the future.

 

The dataset is filtered for the past 12 months of transactions. I've re-created this dataset and in Tableau Desktop applied a filter DATEDIFF('Month', [trans_date], today()) <= 12 to filter for transaction dates w/in the past twelve months. The problem is I'm getting slightly different values between the SQL script value and when I'm using the replicated data (which is the same) and applying the <= 12 formula

 

Is anyone able to tell me how to make the tableau formula spit out the same result? When looking at the data it looks like the SQL dataset might be going back 12 calendar months and excluding current month transactions whereas my formula is capturing current month transactions.

 

SQL formula

DATEADD(m, DATEDIFF(m, 0, [matltran].[trans_date]), 0) AS [Month]

 

Full SQL

 

-- Gets numbers 1-12 

WITH [Tally]

AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS [N]

  FROM [sys].[columns] [a])

  -- Gets last 12 months (Change TOP for more data!)

  , [DateList]

AS (SELECT TOP 12

      DATEADD(m, [Tally].[N] * -1, DATEADD(m, DATEDIFF(m, 0, @Today), 0)) AS [Date]

  FROM [Tally])

  -- Gets all items with qty on hand > 0

  , [AllItems]

AS (SELECT [DateList].[Date]

     , [itemlist].[item]

     , [itemlist].[site_ref]

     , [itemlist].[abc_code]

  FROM [DateList]

    CROSS JOIN

    (

      SELECT [item].[item]

         , [item].[site_ref]

         , [item].[abc_code]

      FROM [dbo].[item_mst] [item]

        INNER JOIN [dbo].[itemwhse_mst] [itemwhse]

          ON [item].[item] = [itemwhse].[item]

            AND [itemwhse].[site_ref] = [item].[site_ref]

      WHERE [itemwhse].[qty_on_hand] > 0

OR [item].[Uf_JBT_SoldPrev1Yr] > 0

OR [item].[Uf_JBT_UsedPrev1yr] > 0

OR [item].[Qty_used_ytd] > 0

OR [itemwhse].[Qty_sold_ytd] > 0

    ) [itemlist] )

  -- Gets all material transactions for the specified time, grouped by month.

  , [Transactions]

AS (SELECT [matltran].[item]

     , [matltran].[site_ref]

     , SUM([matltran].[qty]) * -1 AS [Qty]

     , DATEADD(m, DATEDIFF(m, 0, [matltran].[trans_date]), 0) AS [Month]

  FROM [dbo].[matltran_mst] [matltran]

  WHERE (

       (

         [matltran].[trans_type] = 'I'

         AND [matltran].[ref_type] = 'K' -- Issued to Project

       )

       OR

       (

         [matltran].[trans_type] = 'I'

         AND [matltran].[ref_type] = 'J' -- Issued to Job

       )

       OR

       (

         [matltran].[trans_type] = 'I'

         AND [matltran].[ref_type] = 'S' -- Issued to Production Schedule

       )

       OR

       (

         [matltran].[trans_type] = 'S'

         AND [matltran].[ref_type] = 'O' -- Shipped to Order

       )

       OR

       (

         [matltran].[trans_type] = 'T'

         AND [matltran].[ref_type] = 'T' -- Transferred

         AND [matltran].[qty] < 0

       )

     )

  GROUP BY [matltran].[item]

      , [matltran].[site_ref]

      , DATEADD(m, DATEDIFF(m, 0, [matltran].[trans_date]), 0))

5 respuestas
  1. 30 ago 2022, 16:40

    Can you share this prep file to have a closer look coz without any screenshots or some file I can't help to troubleshoot. Hope you understand that.

0/9000