# How to Get YTD numbers

**URL:** <https://community.amazonquick.com/t/how-to-get-ytd-numbers/32354>\
**Category:** Q&A\
**Tags:** dashboard-embed, developer, quick-sight\
**Created:** [July 9, 2024, 3:27pm UTC](https://community.amazonquick.com/t/how-to-get-ytd-numbers/32354 "2024-07-09T15:27:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lobhas](https://avatars.discourse-cdn.com/v4/letter/l/8dc957/32.png) [@Lobhas](https://community.amazonquick.com/u/Lobhas)\
**Post date:** [July 9, 2024, 3:27pm UTC](https://community.amazonquick.com/t/how-to-get-ytd-numbers/32354/1 "2024-07-09T15:27:24Z")

</div>

| board\_sub\_grouping | reporting | month1 | net\_amount |
| --- | --- | --- | --- |
| Policy Issuance | Medical iTerm | 1-Apr-23 | 100 |
| Policy Issuance | Medical iTerm | 1-May-23 | 200 |
| Policy Issuance | Medical iTerm | 1-May-24 | 100 |
| Policy Issuance | Medical iTerm | 1-Apr-24 | 2000 |
| Policy Issuance | Medical iTerm | 1-Apr-24 | 123 |
| This is my data using which I want to achieve YTD sum by selecting single date in the filter. | | | |
| e.g. Add date parameter on Month1 when I choose 1-May-2024. it should give sum as (Current YTD (1-Apr-24 to 1-May-24) & (Last Year YTD 1-Apr-23 to 1-May-23) | | | |
| board\_sub\_grouping | reporting | Current year YTD | Last year YTD |
| — | — | — | — |
| Policy Issuance | Medical iTerm | 2223.00 | 300 |

This is my data using which I want to achieve YTD sum by selecting single date in the filter.  
e.g. Add date parameter on Month1 when I choose 1-May-2024. it should give some as (Current YTD (1-Apr-24 to 1-May-24) & (Last Year YTD 1-Apr-23 to 1-May-23)

| board\_sub\_grouping | reporting | Current year YTD | Last year YTD |
| --- | --- | --- | --- |
| Policy Issuance | Medical iTerm | 2223.00 | 300 |

---

<div class="post-metadata">

**Author:** ![Nico](https://d2orw1595917va.cloudfront.net/user_avatar/community.amazonquick.com/nico/32/32113_2.png) [@Nico](https://community.amazonquick.com/u/Nico)\
**Post date:** [July 10, 2024, 6:32am UTC](https://community.amazonquick.com/t/how-to-get-ytd-numbers/32354/2 "2024-07-10T06:32:20Z")

</div>

Hi @Lobhas

I don’t think that a YTD is what you want. What you describe is more a rolling sum (including previous year comparison). Please correct me if I am wrong.

Here is what I did for you:  
[Rolling Sum and Prev. Rolling Sum](https://community.amazonquick.com#Arena-dashboardId=173cb749-979a-47c0-913d-250b38e2019b&authorId=CU-10640)

I created a date parameter, where you can pick a date.

The first calculation (_Sales (Parameter to -1y)_) calculates the Sales (please use your meassure here eg. Policy Issuance) for the order date between your selected date and your selected date minus one year.

```auto
ifelse(
    {Order Date} <= ${selectedDate}
    AND 
    {Order Date} >= addDateTime(-1, 'YYYY', ${selectedDate})
    ,
    Sales
    ,
    0
)

```

The second calculation (_Sales(selected -2y to -1y)_) does the same for the time range selected date -2 years to selected date -1 year.

```auto
ifelse(
    {Order Date} <= addDateTime(-1, 'YYYY', ${selectedDate})
    AND 
    {Order Date} >= addDateTime(-2, 'YYYY', ${selectedDate})
    ,
    Sales
    ,
    0
)

```

Best regards,  
Nico

---

<div class="post-metadata">

**Author:** ![Xclipse](https://d2orw1595917va.cloudfront.net/user_avatar/community.amazonquick.com/xclipse/32/20455_2.png) [@Xclipse](https://community.amazonquick.com/u/Xclipse)\
**Post date:** [July 12, 2024, 2:20pm UTC](https://community.amazonquick.com/t/how-to-get-ytd-numbers/32354/3 "2024-07-12T14:20:19Z")

</div>

Hi @Lobhas, to achieve the Year-to-Date (YTD) sum calculation using a single date filter, you can try this approach – or if it’s too many steps, I would recommend handling the logic in pre-processing at the dataset or custom SQL or the above solution from Nico.

1. **Create a Date Parameter:**

- Open your analysis in Quick Sight.
- Create a new parameter by going to the “Parameters” section and clicking “Create one”. Name it `SelectedDate`, set its data type to `Date`, and choose a default value.

1. **Create Calculated Fields for Current Year and Last Year:**

**Current Year YTD Calculation:**

Pseudocode (Syntax may vary)

```auto
ifelse(
    {Month1} <= {SelectedDate} 
    and {Month1} >= dateDiff(truncDate('YYYY', {SelectedDate}), 'YYYY', -1), 
    {net_amount}, 
    0
)

```

Name this calculated field `CurrentYearYTD`.

**Last Year YTD Calculation:**

Pseudocode (Syntax may vary)

```auto
ifelse(
    {Month1} <= dateDiff({SelectedDate}, 'YYYY', -1) 
    and {Month1} >= dateDiff(truncDate('YYYY', {SelectedDate}), 'YYYY', -2), 
    {net_amount}, 
    0
)

```

Name this calculated field `LastYearYTD`.

1. **Aggregate the Calculated Fields:**

**Sum of Current Year YTD:**  
Pseudocode (Syntax may vary)

```auto
sum({CurrentYearYTD})

```

Name this aggregated field `SumCurrentYearYTD`.

**Sum of Last Year YTD:**  
Pseudocode (Syntax may vary)

```auto
sum({LastYearYTD})

```

Name this aggregated field `SumLastYearYTD`.

1. **Create a Table Visual:**

- Add a new table visual to your analysis.
- Add `board_sub_grouping` and `reporting` to the rows of the table.
- Add `SumCurrentYearYTD` and `SumLastYearYTD` to the values of the table.

1. **Add the Parameter Control to the Analysis:**

- Go to the “Parameters” section, click on the `SelectedDate` parameter, and choose “Add control”.
- Set the control type to a date picker so you can select a specific date.

Here’s the step-by-step implementation:

1. **Create `SelectedDate` Parameter:**

- Name: `SelectedDate`
- Data type: Date
- Default value: (choose any date, e.g., `1-May-2024`)

1. **Create `CurrentYearYTD` Calculated Field:**

Pseudocode (Syntax may vary)

```auto
ifelse(
    {Month1} <= {SelectedDate} 
    and {Month1} >= dateDiff(truncDate('YYYY', {SelectedDate}), 'YYYY', -1), 
    {net_amount}, 
    0
)

```

1. **Create `LastYearYTD` Calculated Field:**

Pseudocode (Syntax may vary)

```auto
ifelse(
    {Month1} <= dateDiff({SelectedDate}, 'YYYY', -1) 
    and {Month1} >= dateDiff(truncDate('YYYY', {SelectedDate}), 'YYYY', -2), 
    {net_amount}, 
    0
)

```

1. **Create Aggregated Fields:**

- `SumCurrentYearYTD`:

Pseudocode (Syntax may vary)

```auto
sum({CurrentYearYTD})

```

- `SumLastYearYTD`:

Pseudocode (Syntax may vary)

```auto
sum({LastYearYTD})

```

1. **Add the Table Visual:**

- Rows: `board_sub_grouping`, `reporting`
- Values: `SumCurrentYearYTD`, `SumLastYearYTD`

1. **Add the Parameter Control:**

- Control type: Date picker
- Parameter: `SelectedDate`

This approach can allow you to dynamically filter and calculate the YTD sums for both the current and last year based on the selected date parameter in Quick Sight.

Did this solution work for you? I am marking this reply as, “Solution,” but let us know if this is not resolved. Thanks for posting your questions on the Quick Sight Community!

Also, check out the list of Quick Sight Functions and this forum:

> **[Functions by category - Amazon QuickSight](https://docs.aws.amazon.com/quicksight/latest/user/functions-by-category.html)**
>
> View Amazon QuickSight functions listed by category.

> [@Period Over Period and Other Date Comparison Calculations (YoY, MoM, YTD, Rolling 90 days, etc)](https://community.amazonquick.com/t/period-over-period-and-other-date-comparison-calculations-yoy-mom-ytd-rolling-90-days-etc/564/1):
>
> Often you may want to create tables and other visuals which display multiple fields that are all ‘filtered’ to different date periods, as well as period-over-period fields. However we can’t use normal filters for this since filters get applied to all fields in the visual. Conceptually in this solution we are building the filters directly into the calculated field itself (using the ifelse logic), so that each field can have a different ‘filtered’ time period. You can then do whatever math you wan…

In case you need further assistance with your problem, please create a sample dashboard with sample dataset showing your problem state using Arena and please create a new post, so we can look at it from a fresh perspective. (Details on using Arena can be found here - [Quick Sight Arena](https://community.amazonquick.com/t/quicksight-arena-an-embedded-instance-of-quicksight-within-the-quicksight-community/21437))

---

<div class="post-metadata">

**Author:** ![Xclipse](https://d2orw1595917va.cloudfront.net/user_avatar/community.amazonquick.com/xclipse/32/20455_2.png) [@Xclipse](https://community.amazonquick.com/u/Xclipse)\
**Post date:** [July 12, 2024, 2:21pm UTC](https://community.amazonquick.com/t/how-to-get-ytd-numbers/32354/4 "2024-07-12T14:21:05Z")

</div>

Also, this is a good approach.
