Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Range Creation with filter and all except

Hi Team,

 

I was trying to create a bar chart with the range on X axis.

 

The SQl query is as below:

select count( distinct [COMPANY_NAME]) total,'Range:0-50K' as range from (
select sum(amount) sums,COMPANY_NAME
from Xchange
where TNX_STAT_CODE=4 and [INP_USER_ID]>0
AND appl_date >=(select dateadd(month,datediff(month,0,getdate())-12,0)) AND appl_date <=(select getdate())
and To_CCY='USD'
group by COMPANY_NAME
having sum(amount)<=50000)l
union all
select count( distinct [COMPANY_NAME]) total,'Range:50-100000K' from (
select sum(amount) sums,COMPANY_NAME
from Xchange
where TNX_STAT_CODE=4 and [INP_USER_ID]>0
AND appl_date >=(select dateadd(month,datediff(month,0,getdate())-12,0)) AND appl_date <=(select getdate())
and To_CCY='USD'
group by COMPANY_NAME
having sum(amount)>=50001 and sum(amount)<=100000)l2
union all
select count( distinct [COMPANY_NAME]) total,'Range:Above -100000K' from (
select sum(amount) sums,COMPANY_NAME
from Xchange
where TNX_STAT_CODE=4 and [INP_USER_ID]>0
AND appl_date >=(select dateadd(month,datediff(month,0,getdate())-12,0)) AND appl_date <=(select getdate())
and To_CCY='USD'
group by COMPANY_NAME
having sum(amount)>=100001)l2

 

I would want the range on the x axis and Total as value. 

 

If the sum of the amount is less that 50k it should fall on range 0-50k, else if its greater than 50001 and less that 1lk it should fall in bucket 2, else in bucket above 1lk.

 

  • dax's avatar
    dax
    6 years ago

    Hi Anonymous , 

    You could remove this in filter and try to refer to my sample for details.

    Best Regards,
    Zoe Zhi

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

  • dax's avatar
    dax
    6 years ago

    Hi Anonymous , 

    I think you need to use [TO_CCY]  from summarize table(my Table 2) in slicer  instead of from fact table. If you want to use this field from fact table(Table(2) in my sample), I think you need to create relationship between two tables based on [TO_CCY] .

    Best Regards,
    Zoe Zhi

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

9 Replies

    • Anonymous's avatar
      Anonymous
      Not applicable
      1. We need t create a bar chart with range in the Axis and the distinct count of the company_name on the value field.
      2. The range should have three buckets
        1. 0-50k
        2. 50001k-100k
        3. Above 100k
      3. So, we need to count the companies that have made transactions in these range buckets.
      4. The SQL equivalent for this result required is as below:

      select count( distinct [COMPANY_NAME]) total,'Range:0-50K' as range from (

      select sum(amount) sums,COMPANY_NAME

      from Xchange

      where TNX_STAT_CODE=4  and [INP_USER_ID]>0

      AND  appl_date >=(select dateadd(month,datediff(month,0,getdate())-12,0)) AND appl_date <=(select getdate())

      and To_CCY='USD'

      group by COMPANY_NAME

      having sum(amount)<=50000)l

      union all

      select count( distinct [COMPANY_NAME]) total,'Range:50-100000K' from (

      select sum(amount) sums,COMPANY_NAME

      from Xchange

      where TNX_STAT_CODE=4  and [INP_USER_ID]>0

      AND  appl_date >=(select dateadd(month,datediff(month,0,getdate())-12,0)) AND appl_date <=(select getdate())

      and To_CCY='USD'

      group by COMPANY_NAME

      having sum(amount)>=50001 and sum(amount)<=100000)l2

      union all

      select count( distinct [COMPANY_NAME]) total,'Range:Above -100000K' from (

      select sum(amount) sums,COMPANY_NAME

      from Xchange

      where TNX_STAT_CODE=4  and [INP_USER_ID]>0

      AND  appl_date >=(select dateadd(month,datediff(month,0,getdate())-12,0)) AND appl_date <=(select getdate())

      and To_CCY='USD'

      group by COMPANY_NAME

      having sum(amount)>=100001)l2

      5.The sum of the amount should be grouped based on the company_name and then filtered based on the TNX_STAT_CODE =4 and the INP_USR_ID should be grater than 0 and not blank. Also we will the apply a date filter ie relative date filter for the last 12 calender month based on APPL_DATE.

      6.Action taken: Have created a measure :

      Range Amount:= CALCULATE(SUM('FACT_CONFIG_DERIVED_GTP_PRODUCT'[AMOUNT]),ALLEXCEPT(FACT_CONFIG_DERIVED_GTP_PRODUCT,FACT_CONFIG_DERIVED_GTP_PRODUCT[COMPANY_NAME]))

      7.Then created a column as below:

      Value Range=

      SWITCH (

          TRUE (),

          FACT_CONFIG_DERIVED_GTP_PRODUCT[Range Amount] >= 0

              && FACT_CONFIG_DERIVED_GTP_PRODUCT[Range Amount]<= 50000, "0-50K",

           FACT_CONFIG_DERIVED_GTP_PRODUCT[Range Amount]  >= 50000

              && FACT_CONFIG_DERIVED_GTP_PRODUCT[Range Amount] <= 100000, "51K-100K",

           FACT_CONFIG_DERIVED_GTP_PRODUCT[Range Amount]  >= 100000, "Above 101K"

          

                      )

      8.Then used value Range on X axis and then company name on the value (Count Distinct)

      9.The result is not as expected seeing few records having less count coming in wrong range bucket.

      • Anonymous's avatar
        Anonymous
        Not applicable
        COMPANY_NAMETNX_STAT_CODEINP_USR_IDAMOUNTAPPL_DATETo_CCY
        Apple103542228/09/2020GBP
        Airtel4546500028/03/2020USD
        Mango43461294738328/02/2020USD
        Idea43563328/01/2020USD
        Apple4856000028/09/2019USD
        Airtel5 89998728/01/2019 
        Idea4344454528/09/2019USD
        Mango49845673938/03/2020USD
        Airtel49050002/04/2020USD

         

        Expecting: 0-50k bucket to have count 2, 50001-100k have count 1 and above 100k as count 1

  • Anonymous's avatar
    Anonymous
    Not applicable

    Approach taken by me is : 

     

    Created one measure : 

     

    Range Amount:= CALCULATE(SUM('FACT_CONFIG_DERIVED_GTP_PRODUCT'[AMOUNT]),ALLEXCEPT(FACT_CONFIG_DERIVED_GTP_PRODUCT,FACT_CONFIG_DERIVED_GTP_PRODUCT[COMPANY_NAME]))

     

    Then created one column : 

     

    =
    SWITCH (
    TRUE (),
    FACT_CONFIG_DERIVED_GTP_PRODUCT[Range Amount] >= 0
    && FACT_CONFIG_DERIVED_GTP_PRODUCT[Range Amount]<= 50000, "0-50K",
    FACT_CONFIG_DERIVED_GTP_PRODUCT[Range Amount] >= 50000
    && FACT_CONFIG_DERIVED_GTP_PRODUCT[Range Amount] <= 100000, "51K-100K",
    FACT_CONFIG_DERIVED_GTP_PRODUCT[Range Amount] >= 100000, "Above 101K"

    )

     

    Then on the visual took value range column on the axis and company_name on value n did count distinct, but this is not fectching me expected result.