margin by month or year

  • i would like to count margin by category.
    i have sum and group the category amount.
    how can i count the margin if i want to have option too see by month or by year.

    Country   Year    Month        Total
    Australia  2018   December  80000
    Australia  2018   February    50000
    China       2018   March        20000
    India        2018    March        40000

  • girl_bj - Thursday, January 31, 2019 6:52 PM

    i would like to count margin by category.
    i have sum and group the category amount.
    how can i count the margin if i want to have option too see by month or by year.

    Country   Year    Month        Total
    Australia  2018   December  80000
    Australia  2018   February    50000
    China       2018   March        20000
    India        2018    March        40000

    this is my current query, is my calculation right here?

    select year,month,country,total,[$]
    ,[$] / totalAll as [%]
    from(

    select year,month,country,total,totalAll
    ,totalAll-total as [$]
    from(

    select x.year,x.month,country,sum(total)total
    from tableA x
    inner join (select year,month,sum(total)totalAll from tableA y where y.year=year and y.month=month group by year,month)zz
    on x.year=zz.year and x.month=zz.month
    group by x.year,x.month,country

    )a
    )b

  • girl_bj - Thursday, January 31, 2019 6:52 PM

    i would like to count margin by category.
    i have sum and group the category amount.
    how can i count the margin if i want to have option too see by month or by year.

    Country   Year    Month        Total
    Australia  2018   December  80000
    Australia  2018   February    50000
    China       2018   March        20000
    India        2018    March        40000

    What do you mean by "margin"? I'm confused. Margin is usually ([Cost] - [Price]) * [UnitsSold], right? Are you doing this in SQL Server? Because if you're doing this in DAX/Excel, then the answer is different.

  • pietlinden - Thursday, January 31, 2019 8:29 PM

    girl_bj - Thursday, January 31, 2019 6:52 PM

    i would like to count margin by category.
    i have sum and group the category amount.
    how can i count the margin if i want to have option too see by month or by year.

    Country   Year    Month        Total
    Australia  2018   December  80000
    Australia  2018   February    50000
    China       2018   March        20000
    India        2018    March        40000

    What do you mean by "margin"? I'm confused. Margin is usually ([Cost] - [Price]) * [UnitsSold], right? Are you doing this in SQL Server? Because if you're doing this in DAX/Excel, then the answer is different.

    Yes, im doing it in SQL.

  • Okay, Can you post some sample data and expected output? I'm still not clear on what you're trying to do.

  • pietlinden - Thursday, January 31, 2019 8:56 PM

    Okay, Can you post some sample data and expected output? I'm still not clear on what you're trying to do.

    this is my current query, is my calculation right here? I would like to get the margin. Not so sure on the calculation.

    select year,month,country,total,totalAll,[$]
    ,[$] / totalAll as [%]
    from(
    select year,month,country,total,totalAll
    ,totalAll-total as [$]
    from(
    select x.year,x.month,country,sum(total)total,totalAll
    from #tableA x
    inner join (select year,month,sum(total)totalAll from #tableA y where y.year=year and y.month=month group by year,month)zz
    on x.year=zz.year and x.month=zz.month
    group by x.year,x.month,country,totalAll
    )a)b

  • When I ran your query, it said it couldn't find the table...
    And you never defined what you mean by Margin. Without TotalCost, how do you define Margin?

  • pietlinden - Thursday, January 31, 2019 9:12 PM

    When I ran your query, it said it couldn't find the table...
    And you never defined what you mean by Margin. Without TotalCost, how do you define Margin?

    I push it to temp table. 
    The totalcost I sum it by month. Is that right?

  • Since you never really explained what you're trying to accomplish, I have no idea.

    If you want an answer, please read this article and follow the directions :Forum Etiquette: How to post data/code on a forum to get the best help

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply