-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.html
More file actions
1129 lines (1086 loc) · 50.4 KB
/
README.html
File metadata and controls
1129 lines (1086 loc) · 50.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.4.551">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>README</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
vertical-align: middle;
}
/* CSS for syntax highlighting */
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
}
pre.numberSource { margin-left: 3em; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
</style>
<script src="README_files/libs/clipboard/clipboard.min.js"></script>
<script src="README_files/libs/quarto-html/quarto.js"></script>
<script src="README_files/libs/quarto-html/popper.min.js"></script>
<script src="README_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="README_files/libs/quarto-html/anchor.min.js"></script>
<link href="README_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="README_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="README_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="README_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="README_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
</head>
<body class="fullcontent">
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title">README</h1>
</div>
<div class="quarto-title-meta">
</div>
</header>
<section id="fpar-a-business-intelligence-toolkit-for-financial-planning-analysis-fpa" class="level1">
<h1>fpaR: A Business Intelligence Toolkit for Financial Planning & Analysis (FP&A)</h1>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p><code>fpaR</code> is a collection of business intelligence tools designed to simplify common <strong>financial planning and analysis (FP&A)</strong> tasks such as time intelligence calculations, customer, vendor or product segmentation, and factor/variance analysis.</p>
<p>The package is inspired by best practices from a collection of blogs, books, industry research, and hands-on work experience, consolidating frequently performed business analyses into a fast, efficient, and reusable framework.</p>
<p>In particular, the time intelligence functions are heavily inspired by <a href="https://www.sqlbi.com/">PowerBI DAX</a> functions.</p>
<p>Under the hood, these functions are built upon the great foundations of:</p>
<ul>
<li><a href="https://dbplyr.tidyverse.org/">dbplyr</a><br>
</li>
<li><a href="https://github.com/duckdb/duckdb-r">duckdb</a></li>
<li><a href="https://lubridate.tidyverse.org/">lubridate</a></li>
</ul>
<p><code>fpaR</code> is designed to seamlessly work with either tibbles or modern databases (DuckDB, Snowflake, SQLite, etc) with a unified syntax.</p>
<p>Even if you are working with tibbles, most functions are optimized to leverage <a href="https://github.com/duckdb/duckdb-r">DuckDB</a> for superior speed and performance.<a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"><sup>1</sup></a></p>
<p>By default most <code>fpaR</code> function returns a lazy DBI object which you can return as a tibble with <code>dplyr::collect()</code></p>
</section>
<section id="key-features-benefits" class="level2">
<h2 class="anchored" data-anchor-id="key-features-benefits">Key features & benefits</h2>
<ul>
<li><strong>Unified syntax</strong> regardless if your data is in a tibble or a database</li>
<li><strong>Optimized for speed and scale</strong> your data is converted to a local <a href="https://github.com/duckdb/duckdb-r">duckdb</a> database to scale your calculations</li>
<li><strong>Instant clarity</strong> as every function summarizes its transformation actions so that you can understand and validate the functions results</li>
</ul>
</section>
<section id="installation" class="level2">
<h2 class="anchored" data-anchor-id="installation">Installation</h2>
<p>Install the development version of <code>fpaR</code> from GitHub:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Install using pak or install.package()</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>pak<span class="sc">::</span><span class="fu">pak</span>(<span class="st">"alejandrohagan/fpaR"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="what-is-in-fpar" class="level2">
<h2 class="anchored" data-anchor-id="what-is-in-fpar">What is in fpaR?</h2>
<p>There are 3 main categories of functions: - Time intelligence (Ready for testing and feedback) - Segmentation strategies (work in progress) - Factor Analysis (work in progress)</p>
<section id="time-intelligence" class="level3">
<h3 class="anchored" data-anchor-id="time-intelligence">Time intelligence</h3>
<p><code>fpaR</code> provides readily available functions for most time intelligence analysis such as <strong>Year-over-Year (YoY)</strong>, <strong>Month-to-Date (MTD)</strong>, and <strong>Current Year-to-Date over Previous Year-to-Date (YoYTD)</strong> analysis.</p>
<p>These functions are designed to quickly answer most common time intelligence related analysis in a consistent, fast and transparent way.</p>
<p><strong>Key benefits:</strong></p>
<ul>
<li><p><strong>Auto-fill missing dates</strong>: Ensures no missing periods in your datasets so that right period comparisons are performed</p></li>
<li><p><strong>Flexible calendar options</strong>: Handle comparisons based on a <strong>standard</strong> or <strong>5-5-4</strong> fiscal calendar to accommodate different reporting frameworks</p></li>
<li><p><strong>Period imbalance indicator</strong>: When comparing periods with dates imbalance, the time intelligence functions will alert you to the type and number of period imbalances to ensure you are aware of misleading likewise comparisons</p></li>
</ul>
<p>Below is the full list of time intelligence functions:</p>
<div class="cell">
<div class="cell-output-display">
<!-- preamble start -->
<script>
function styleCell_jm7gp4jd5b4nq4h2htc6(i, j, css_id) {
var table = document.getElementById("tinytable_jm7gp4jd5b4nq4h2htc6");
var cell = table.rows[i]?.cells[j]; // Safe navigation to avoid errors
if (cell) {
console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
cell.classList.add(css_id);
} else {
console.warn(`Cell at (${i}, ${j}) not found.`);
}
}
function insertSpanRow(i, colspan, content) {
var table = document.getElementById('tinytable_jm7gp4jd5b4nq4h2htc6');
var newRow = table.insertRow(i);
var newCell = newRow.insertCell(0);
newCell.setAttribute("colspan", colspan);
// newCell.innerText = content;
// this may be unsafe, but innerText does not interpret <br>
newCell.innerHTML = content;
}
function spanCell_jm7gp4jd5b4nq4h2htc6(i, j, rowspan, colspan) {
var table = document.getElementById("tinytable_jm7gp4jd5b4nq4h2htc6");
const targetRow = table.rows[i];
const targetCell = targetRow.cells[j];
for (let r = 0; r < rowspan; r++) {
// Only start deleting cells to the right for the first row (r == 0)
if (r === 0) {
// Delete cells to the right of the target cell in the first row
for (let c = colspan - 1; c > 0; c--) {
if (table.rows[i + r].cells[j + c]) {
table.rows[i + r].deleteCell(j + c);
}
}
}
// For rows below the first, delete starting from the target column
if (r > 0) {
for (let c = colspan - 1; c >= 0; c--) {
if (table.rows[i + r] && table.rows[i + r].cells[j]) {
table.rows[i + r].deleteCell(j);
}
}
}
}
// Set rowspan and colspan of the target cell
targetCell.rowSpan = rowspan;
targetCell.colSpan = colspan;
}
// tinytable span after
window.addEventListener('load', function () {
var cellsToStyle = [
// tinytable style arrays after
{ positions: [ { i: 21, j: 0 }, { i: 21, j: 1 }, { i: 21, j: 2 }, { i: 21, j: 3 }, { i: 21, j: 4 }, ], css_id: 'tinytable_css_6kk4nec0d1qwlgp7ihku',},
{ positions: [ { i: 0, j: 0 }, { i: 0, j: 1 }, { i: 0, j: 2 }, { i: 0, j: 3 }, { i: 0, j: 4 }, ], css_id: 'tinytable_css_0mjjbdooqbmzxyr2xzxs',},
];
// Loop over the arrays to style the cells
cellsToStyle.forEach(function (group) {
group.positions.forEach(function (cell) {
styleCell_jm7gp4jd5b4nq4h2htc6(cell.i, cell.j, group.css_id);
});
});
});
</script>
<style>
/* tinytable css entries after */
.table td.tinytable_css_6kk4nec0d1qwlgp7ihku, .table th.tinytable_css_6kk4nec0d1qwlgp7ihku { border-bottom: solid #d3d8dc 0.1em; }
.table td.tinytable_css_0mjjbdooqbmzxyr2xzxs, .table th.tinytable_css_0mjjbdooqbmzxyr2xzxs { border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
</style>
<div class="container">
<table class="table table-borderless" id="tinytable_jm7gp4jd5b4nq4h2htc6" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead>
<tr>
<th scope="col">short_name</th>
<th scope="col">description</th>
<th scope="col">shift</th>
<th scope="col">aggregate</th>
<th scope="col">compare</th>
</tr>
</thead>
<tbody>
<tr>
<td>YoY</td>
<td>Full Year over Year</td>
<td></td>
<td></td>
<td>X</td>
</tr>
<tr>
<td>YTD</td>
<td>Year-to-Date</td>
<td></td>
<td>X</td>
<td></td>
</tr>
<tr>
<td>PYTD</td>
<td>Prior Year-to-Date amount</td>
<td>X</td>
<td>X</td>
<td></td>
</tr>
<tr>
<td>YoYTD</td>
<td>Current Year-to-Date over Prior Year-to-Date</td>
<td>X</td>
<td>X</td>
<td>X</td>
</tr>
<tr>
<td>YTDOPY</td>
<td>Year-to-Date over Full Previous Year</td>
<td>X</td>
<td>X</td>
<td>X</td>
</tr>
<tr>
<td>QoQ</td>
<td>Full Quarter over Quarter</td>
<td></td>
<td></td>
<td>X</td>
</tr>
<tr>
<td>QTD</td>
<td>Quarter-to-Date</td>
<td></td>
<td>X</td>
<td></td>
</tr>
<tr>
<td>PQTD</td>
<td>Prior Quarter-to-Date</td>
<td>X</td>
<td>X</td>
<td></td>
</tr>
<tr>
<td>QOQTD</td>
<td>Quarter-over-Quarter-to-Date</td>
<td>X</td>
<td>X</td>
<td>X</td>
</tr>
<tr>
<td>QTDOPQ</td>
<td>Quarter-to-Date over Full Previous Quarter</td>
<td>X</td>
<td>X</td>
<td>X</td>
</tr>
<tr>
<td>MTD</td>
<td>Month-to-Date</td>
<td></td>
<td>X</td>
<td></td>
</tr>
<tr>
<td>MoM</td>
<td>Full Month over Full Month</td>
<td></td>
<td></td>
<td>X</td>
</tr>
<tr>
<td>MoMTD</td>
<td>Current Month-to-Date over Prior Month-to-Date</td>
<td>X</td>
<td>X</td>
<td>X</td>
</tr>
<tr>
<td>PMTD</td>
<td>Prior Month's MTD amount</td>
<td>X</td>
<td>X</td>
<td></td>
</tr>
<tr>
<td>MTDOPM</td>
<td>Month-to-Date over Full Previous Month</td>
<td>X</td>
<td>X</td>
<td>X</td>
</tr>
<tr>
<td>WTD</td>
<td>Week-to-Date</td>
<td></td>
<td>X</td>
<td></td>
</tr>
<tr>
<td>WoW</td>
<td>Full Week over Full Week</td>
<td></td>
<td></td>
<td>X</td>
</tr>
<tr>
<td>WoWTD</td>
<td>Current Week-to-Date over Prior Week-to-Date</td>
<td>X</td>
<td>X</td>
<td>X</td>
</tr>
<tr>
<td>PWTD</td>
<td>Prior Week-to-Date</td>
<td>X</td>
<td>X</td>
<td></td>
</tr>
<tr>
<td>ATD</td>
<td>cumlaitve total from inception to date</td>
<td></td>
<td>x</td>
<td></td>
</tr>
<tr>
<td>DoD</td>
<td>Full Day over Full Day</td>
<td></td>
<td></td>
<td>X</td>
</tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
</section>
</section>
<section id="how-to-use-fpar" class="level2">
<h2 class="anchored" data-anchor-id="how-to-use-fpar">How to use fpaR?</h2>
<section id="time-intelligence-1" class="level3">
<h3 class="anchored" data-anchor-id="time-intelligence-1">Time Intelligence</h3>
<p>When you execute a time intelligence function, it will return a <code>ti</code> class object with a custom print method that explains what the function is doing and a summary of transformation steps and the calendar attributes</p>
<p>You will see a print method that explains the function’s actions, details the calendar’s attributes, summarizes the main transformation steps and lists out possible next actions</p>
<p>To return a tibble of results, pass the ti object through to <code>calculate()</code></p>
<div class="cell">
<div class="sourceCode cell-code" id="annotated-cell-2"><pre class="sourceCode numberSource r code-annotation-code number-lines code-with-copy code-annotated"><code class="sourceCode r"><a class="code-annotation-anchor" data-target-cell="annotated-cell-2" data-target-annotation="1" onclick="event.preventDefault();" href="">1</a><span id="annotated-cell-2-1" class="code-annotation-target"><a href="#annotated-cell-2-1"></a>sales <span class="sc">|></span></span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-2" data-target-annotation="2" onclick="event.preventDefault();" href="">2</a><span id="annotated-cell-2-2" class="code-annotation-target"><a href="#annotated-cell-2-2"></a> <span class="fu">mtd</span>(<span class="at">.date=</span>order_date,<span class="at">.value =</span> margin,<span class="at">calendar_type =</span> <span class="st">"standard"</span>) <span class="sc">|></span></span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-2" data-target-annotation="3" onclick="event.preventDefault();" href="">3</a><span id="annotated-cell-2-3" class="code-annotation-target"><a href="#annotated-cell-2-3"></a> <span class="fu">calculate</span>()</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-annotation">
<dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-2" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-2" data-code-lines="1" data-code-annotation="1">Take your dataset</span>
</dd>
<dt data-target-cell="annotated-cell-2" data-target-annotation="2">2</dt>
<dd>
<span data-code-cell="annotated-cell-2" data-code-lines="2" data-code-annotation="2">Calculate the order date’s month to date margin using a standard calendar</span>
</dd>
<dt data-target-cell="annotated-cell-2" data-target-annotation="3">3</dt>
<dd>
<span data-code-cell="annotated-cell-2" data-code-lines="3" data-code-annotation="3">Return the results</span>
</dd>
</dl>
</div>
<div class="cell-output cell-output-stdout">
<pre><code># Source: SQL [?? x 7]
# Database: DuckDB v1.1.3 [hagan@Linux 6.12.10-76061203-generic:R 4.4.3//tmp/RtmpShk9gR/file36b5a5448604f]
# Ordered by: date
year month date margin missing_date_indicator mtd_margin
<dbl> <dbl> <date> <dbl> <dbl> <dbl>
1 2021 8 2021-08-01 0 1 0
2 2021 8 2021-08-02 0 1 0
3 2021 8 2021-08-03 0 1 0
4 2021 8 2021-08-04 2091. 0 2091.
5 2021 8 2021-08-05 112. 0 2203.
6 2021 8 2021-08-06 71.2 0 2274.
7 2021 8 2021-08-07 4406. 0 6681.
8 2021 8 2021-08-08 0 1 6681.
9 2021 8 2021-08-09 2044. 0 8724.
10 2021 8 2021-08-10 6847. 0 15572.
# ℹ more rows
# ℹ 1 more variable: days_in_current_period <dbl></code></pre>
</div>
</div>
<p>If you using a tibble, under the hood, <code>fpaR</code> is converting your data to a <a href="https://github.com/duckdb/duckdb-r">duckdb</a> database.</p>
<p>If your data is a datable, the package will leverage <a href="https://dbplyr.tidyverse.org/">dbplyr</a> to execute all the calculations.</p>
<p>Either case use <code>dplyr::collect()</code> to return your results to a local tibble.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>sales <span class="sc">|></span> </span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mtd</span>(<span class="at">.date=</span>order_date,<span class="at">.value =</span> margin,<span class="at">calendar_type =</span> <span class="st">"standard"</span>) <span class="sc">|></span> </span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">calculate</span>() <span class="sc">|></span> </span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">collect</span>() <span class="sc">|></span> </span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">head</span>(<span class="dv">10</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 10 × 7
year month date margin missing_date_indicator mtd_margin
<dbl> <dbl> <date> <dbl> <dbl> <dbl>
1 2022 5 2022-05-01 0 1 0
2 2022 5 2022-05-02 1106. 0 1106.
3 2022 5 2022-05-03 19356. 0 20462.
4 2022 5 2022-05-04 3595. 0 24057.
5 2022 5 2022-05-05 8027. 0 32084.
6 2022 5 2022-05-06 3188. 0 35272.
7 2022 5 2022-05-07 18473. 0 53744.
8 2022 5 2022-05-08 0 1 53744.
9 2022 5 2022-05-09 1638. 0 55383.
10 2022 5 2022-05-10 3084. 0 58466.
# ℹ 1 more variable: days_in_current_period <dbl></code></pre>
</div>
</div>
</section>
<section id="what-if-you-need-the-analysis-at-the-group-level" class="level3">
<h3 class="anchored" data-anchor-id="what-if-you-need-the-analysis-at-the-group-level">what if you need the analysis at the group level?</h3>
<p>Simply pass through the groups that you want with <code>dplyr::group_by()</code> and time intelligence function will create a custom calendar for each group level.</p>
<div class="cell">
<div class="sourceCode cell-code" id="annotated-cell-4"><pre class="sourceCode numberSource r code-annotation-code number-lines code-with-copy code-annotated"><code class="sourceCode r"><a class="code-annotation-anchor" data-target-cell="annotated-cell-4" data-target-annotation="1" onclick="event.preventDefault();" href="">1</a><span id="annotated-cell-4-1" class="code-annotation-target"><a href="#annotated-cell-4-1"></a>sales <span class="sc">|></span></span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-4" data-target-annotation="2" onclick="event.preventDefault();" href="">2</a><span id="annotated-cell-4-2" class="code-annotation-target"><a href="#annotated-cell-4-2"></a> dplyr<span class="sc">::</span><span class="fu">group_by</span>(customer_key,store_key) <span class="sc">|></span></span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-4" data-target-annotation="3" onclick="event.preventDefault();" href="">3</a><span id="annotated-cell-4-3" class="code-annotation-target"><a href="#annotated-cell-4-3"></a> <span class="fu">yoy</span>(<span class="at">.date=</span>order_date,<span class="at">.value =</span> margin,<span class="at">calendar_type =</span> <span class="st">"standard"</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-annotation">
<dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-4" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-4" data-code-lines="1" data-code-annotation="1">Take your dataset</span>
</dd>
<dt data-target-cell="annotated-cell-4" data-target-annotation="2">2</dt>
<dd>
<span data-code-cell="annotated-cell-4" data-code-lines="2" data-code-annotation="2">Select the groups you want with <code>group_by()</code></span>
</dd>
<dt data-target-cell="annotated-cell-4" data-target-annotation="3">3</dt>
<dd>
<span data-code-cell="annotated-cell-4" data-code-lines="3" data-code-annotation="3">Prints the sumamrized instructions the actions the functions performs to calculate the year over year analysis</span>
</dd>
</dl>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>• The calendar aggregated order_date to the year time unit
• A standard calendar is created with 2 groups
• Calendar ranges from 2021-05-18 to 2024-04-20
• 222 days were missing and replaced with 0
• New date column date and year was created from order_date</code></pre>
</div>
</div>
</section>
</section>
<section id="why-do-we-need-this-package-when-we-have-lubridate" class="level2">
<h2 class="anchored" data-anchor-id="why-do-we-need-this-package-when-we-have-lubridate">Why do we need this package when we have lubridate?</h2>
<p><a href="https://lubridate.tidyverse.org/">Lubridate</a> is an excellent package and is heavily used by the package. The issue isn’t lubridate but rather issues you may not be aware of in your package.</p>
<p>Time-based comparisons, such as Year-over-Year (YoY), Quarter-over-Quarter (QoQ), and Month-to-Date (MTD), are common for tracking business performance. However, they come with challenges:</p>
<ul>
<li><p>Many datasets <strong>do not have continuous dates</strong>, especially if data is recorded only on business days or for active transactions</p></li>
<li><p>Period imbalances between periods (Eg. the different number of days between February vs. January) can create misleading analysis or trends</p></li>
<li><p>Your analysis may need to reference a non-standard calendar such as a 5-5-4, 4-4-5, or 13 month calendar</p></li>
<li><p>Your data may be in excel sheets, csv or databases and you need to inter-operable framework to switch between all your data types</p></li>
</ul>
<section id="continuous-dates" class="level3">
<h3 class="anchored" data-anchor-id="continuous-dates">Continuous Dates</h3>
<div class="cell">
<div class="cell-output-display">
<!-- preamble start -->
<script>
function styleCell_d2rxr6u6s85nxxxq4esu(i, j, css_id) {
var table = document.getElementById("tinytable_d2rxr6u6s85nxxxq4esu");
var cell = table.rows[i]?.cells[j]; // Safe navigation to avoid errors
if (cell) {
console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
cell.classList.add(css_id);
} else {
console.warn(`Cell at (${i}, ${j}) not found.`);
}
}
function insertSpanRow(i, colspan, content) {
var table = document.getElementById('tinytable_d2rxr6u6s85nxxxq4esu');
var newRow = table.insertRow(i);
var newCell = newRow.insertCell(0);
newCell.setAttribute("colspan", colspan);
// newCell.innerText = content;
// this may be unsafe, but innerText does not interpret <br>
newCell.innerHTML = content;
}
function spanCell_d2rxr6u6s85nxxxq4esu(i, j, rowspan, colspan) {
var table = document.getElementById("tinytable_d2rxr6u6s85nxxxq4esu");
const targetRow = table.rows[i];
const targetCell = targetRow.cells[j];
for (let r = 0; r < rowspan; r++) {
// Only start deleting cells to the right for the first row (r == 0)
if (r === 0) {
// Delete cells to the right of the target cell in the first row
for (let c = colspan - 1; c > 0; c--) {
if (table.rows[i + r].cells[j + c]) {
table.rows[i + r].deleteCell(j + c);
}
}
}
// For rows below the first, delete starting from the target column
if (r > 0) {
for (let c = colspan - 1; c >= 0; c--) {
if (table.rows[i + r] && table.rows[i + r].cells[j]) {
table.rows[i + r].deleteCell(j);
}
}
}
}
// Set rowspan and colspan of the target cell
targetCell.rowSpan = rowspan;
targetCell.colSpan = colspan;
}
// tinytable span after
window.addEventListener('load', function () {
var cellsToStyle = [
// tinytable style arrays after
{ positions: [ { i: 7, j: 0 }, { i: 7, j: 1 }, ], css_id: 'tinytable_css_rnsm4gnw1w5q5lqr6rf1',},
{ positions: [ { i: 0, j: 0 }, { i: 0, j: 1 }, ], css_id: 'tinytable_css_34zbwr7tcewpqb3v4evc',},
];
// Loop over the arrays to style the cells
cellsToStyle.forEach(function (group) {
group.positions.forEach(function (cell) {
styleCell_d2rxr6u6s85nxxxq4esu(cell.i, cell.j, group.css_id);
});
});
});
</script>
<style>
/* tinytable css entries after */
.table td.tinytable_css_rnsm4gnw1w5q5lqr6rf1, .table th.tinytable_css_rnsm4gnw1w5q5lqr6rf1 { border-bottom: solid #d3d8dc 0.1em; }
.table td.tinytable_css_34zbwr7tcewpqb3v4evc, .table th.tinytable_css_34zbwr7tcewpqb3v4evc { border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
</style>
<div class="container">
<table class="table table-borderless" id="tinytable_d2rxr6u6s85nxxxq4esu" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead>
<tr>
<th scope="col">order_date</th>
<th scope="col">margin</th>
</tr>
</thead>
<tbody>
<tr>
<td>2024-01-01</td>
<td>1200</td>
</tr>
<tr>
<td>2024-01-03</td>
<td>1100</td>
</tr>
<tr>
<td>2024-01-06</td>
<td>1300</td>
</tr>
<tr>
<td>2024-01-07</td>
<td>900</td>
</tr>
<tr>
<td>2024-01-08</td>
<td>1200</td>
</tr>
<tr>
<td>2024-01-09</td>
<td>850</td>
</tr>
<tr>
<td>2024-01-11</td>
<td>1450</td>
</tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>If we use <code>dplyr::lag()</code> to compare <strong>Day-over-Day (DoD)</strong> revenue, we would be missing <code>2024-01-02</code>, <code>2024-01-04</code>, and <code>2024-01-05</code> which will lead to incorrect answers or trends.</p>
<p>To correct this, <code>fpaR</code> will complete your calendar for each group for the missing periods to ensure there are no missing periods when calculating trends.</p>
</section>
</section>
<section id="issue-2" class="level2">
<h2 class="anchored" data-anchor-id="issue-2">Issue 2</h2>
<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 10 × 5
date year month pmtd_margin days_in_comparison_period
<date> <dbl> <dbl> <dbl> <dbl>
1 2022-02-28 2022 2 142668. 31
2 2022-02-27 2022 2 132202. 27
3 2022-02-26 2022 2 129436. 26
4 2022-02-25 2022 2 123449. 25
5 2022-02-24 2022 2 112772. 24
6 2022-02-23 2022 2 112085. 23
7 2022-02-22 2022 2 112085. 22
8 2022-02-21 2022 2 101241. 21
9 2022-02-20 2022 2 97958. 20
10 2022-02-19 2022 2 95431. 19</code></pre>
</div>
</div>
<ul>
<li>TBD</li>
</ul>
</section>
<section id="issue-3" class="level2">
<h2 class="anchored" data-anchor-id="issue-3">Issue 3</h2>
<ul>
<li>TBD</li>
</ul>
</section>
<section id="datasets" class="level2">
<h2 class="anchored" data-anchor-id="datasets">Datasets</h2>
<ul>
<li>This package leverages the <a href="https://github.com/alejandrohagan/contoso">Contoso</a> package for its analysis. The contoso datasets are fictional business transaction of Contoso toy company which are helpful for business intelligence related analysis</li>
</ul>
<p>This is an <strong>active work-in-progress</strong>, and feedback, testing, and contributions are welcome!</p>
</section>
<section id="future-capabilities" class="level2">
<h2 class="anchored" data-anchor-id="future-capabilities">Future capabilities</h2>
<section id="segmentation-strategies" class="level3">
<h3 class="anchored" data-anchor-id="segmentation-strategies"><strong>Segmentation Strategies</strong></h3>
<p>Provides functions to segment and categorize your data into meaningful business categories.</p>
<section id="example-segmentation-methods" class="level4">
<h4 class="anchored" data-anchor-id="example-segmentation-methods"><strong>Example Segmentation Methods:</strong></h4>
<ul>
<li><strong>ABC Classification</strong> – Categorizing products/customers based on revenue contribution</li>
<li><strong>Cohort Analysis</strong> - Categorize your data by a shared time based attribute to track over time</li>
<li><strong>New vs. Returning</strong> – Distinguishing first-time buyers from repeat customers</li>
<li><strong>K-means Clustering</strong> – Grouping data points based on patterns</li>
<li><strong>UMAP (Uniform Manifold Approximation and Projection)</strong> – Dimensionaltiy reduction for clustering</li>
</ul>
<hr>
</section>
</section>
<section id="factor-variation-analysis" class="level3">
<h3 class="anchored" data-anchor-id="factor-variation-analysis"><strong>Factor / Variation Analysis</strong></h3>
<p>Breaks down revenue or cost changes into <strong>price, volume, and mix effects</strong>.</p>
<section id="use-cases" class="level4">
<h4 class="anchored" data-anchor-id="use-cases"><strong>Use Cases:</strong></h4>
<ul>
<li>Analyzing revenue growth <strong>due to price increases vs. increased sales volume</strong></li>
<li>Measuring the impact of <strong>product mix changes on profitability</strong>.</li>
</ul>
<hr>
</section>
</section>
</section>
<section id="additional-references-and-inspirations" class="level2">
<h2 class="anchored" data-anchor-id="additional-references-and-inspirations">Additional references and inspirations</h2>
<ul>
<li><a href="https://github.com/PeerChristensen/cohorts">PeerChristensen’s Cohort Package</a></li>
</ul>
</section>
</section>
<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document" role="doc-endnotes"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>
<ol>
<li id="fn1"><p>I plan use <a href="https://duckplyr.tidyverse.org/index.html">duckplyr</a> once it expands support for lubricate functions<a href="#fnref1" class="footnote-back" role="doc-backlink">↩︎</a></p></li>
</ol>
</section></div></main>
<!-- /main column -->
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const toggleBodyColorMode = (bsSheetEl) => {
const mode = bsSheetEl.getAttribute("data-mode");
const bodyEl = window.document.querySelector("body");
if (mode === "dark") {
bodyEl.classList.add("quarto-dark");
bodyEl.classList.remove("quarto-light");
} else {
bodyEl.classList.add("quarto-light");
bodyEl.classList.remove("quarto-dark");
}
}
const toggleBodyColorPrimary = () => {
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
if (bsSheetEl) {
toggleBodyColorMode(bsSheetEl);
}
}
toggleBodyColorPrimary();
const icon = "";
const anchorJS = new window.AnchorJS();
anchorJS.options = {
placement: 'right',
icon: icon
};
anchorJS.add('.anchored');
const isCodeAnnotation = (el) => {
for (const clz of el.classList) {
if (clz.startsWith('code-annotation-')) {
return true;
}
}
return false;
}
const clipboard = new window.ClipboardJS('.code-copy-button', {
text: function(trigger) {
const codeEl = trigger.previousElementSibling.cloneNode(true);
for (const childEl of codeEl.children) {
if (isCodeAnnotation(childEl)) {
childEl.remove();
}
}
return codeEl.innerText;
}
});
clipboard.on('success', function(e) {
// button target
const button = e.trigger;
// don't keep focus
button.blur();
// flash "checked"
button.classList.add('code-copy-button-checked');
var currentTitle = button.getAttribute("title");
button.setAttribute("title", "Copied!");
let tooltip;
if (window.bootstrap) {
button.setAttribute("data-bs-toggle", "tooltip");
button.setAttribute("data-bs-placement", "left");
button.setAttribute("data-bs-title", "Copied!");
tooltip = new bootstrap.Tooltip(button,
{ trigger: "manual",
customClass: "code-copy-button-tooltip",
offset: [0, -8]});
tooltip.show();
}
setTimeout(function() {
if (tooltip) {
tooltip.hide();
button.removeAttribute("data-bs-title");
button.removeAttribute("data-bs-toggle");
button.removeAttribute("data-bs-placement");
}
button.setAttribute("title", currentTitle);
button.classList.remove('code-copy-button-checked');
}, 1000);
// clear code selection
e.clearSelection();
});
var localhostRegex = new RegExp(/^(?:http|https):\/\/localhost\:?[0-9]*\//);
var mailtoRegex = new RegExp(/^mailto:/);
var filterRegex = new RegExp('/' + window.location.host + '/');
var isInternal = (href) => {
return filterRegex.test(href) || localhostRegex.test(href) || mailtoRegex.test(href);
}
// Inspect non-navigation links and adorn them if external
var links = window.document.querySelectorAll('a[href]:not(.nav-link):not(.navbar-brand):not(.toc-action):not(.sidebar-link):not(.sidebar-item-toggle):not(.pagination-link):not(.no-external):not([aria-hidden]):not(.dropdown-item):not(.quarto-navigation-tool)');
for (var i=0; i<links.length; i++) {
const link = links[i];
if (!isInternal(link.href)) {
// undo the damage that might have been done by quarto-nav.js in the case of
// links that we want to consider external
if (link.dataset.originalHref !== undefined) {
link.href = link.dataset.originalHref;
}
}
}
function tippyHover(el, contentFn, onTriggerFn, onUntriggerFn) {
const config = {
allowHTML: true,
maxWidth: 500,
delay: 100,
arrow: false,
appendTo: function(el) {
return el.parentElement;
},
interactive: true,
interactiveBorder: 10,
theme: 'quarto',
placement: 'bottom-start',
};
if (contentFn) {
config.content = contentFn;
}
if (onTriggerFn) {
config.onTrigger = onTriggerFn;
}
if (onUntriggerFn) {
config.onUntrigger = onUntriggerFn;
}
window.tippy(el, config);
}
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
for (var i=0; i<noterefs.length; i++) {
const ref = noterefs[i];
tippyHover(ref, function() {
// use id or data attribute instead here
let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
try { href = new URL(href).hash; } catch {}
const id = href.replace(/^#\/?/, "");
const note = window.document.getElementById(id);
if (note) {
return note.innerHTML;
} else {
return "";
}
});
}
const xrefs = window.document.querySelectorAll('a.quarto-xref');
const processXRef = (id, note) => {
// Strip column container classes
const stripColumnClz = (el) => {
el.classList.remove("page-full", "page-columns");
if (el.children) {
for (const child of el.children) {
stripColumnClz(child);
}
}
}
stripColumnClz(note)
if (id === null || id.startsWith('sec-')) {
// Special case sections, only their first couple elements
const container = document.createElement("div");
if (note.children && note.children.length > 2) {
container.appendChild(note.children[0].cloneNode(true));
for (let i = 1; i < note.children.length; i++) {
const child = note.children[i];
if (child.tagName === "P" && child.innerText === "") {
continue;
} else {
container.appendChild(child.cloneNode(true));
break;
}
}
if (window.Quarto?.typesetMath) {
window.Quarto.typesetMath(container);
}
return container.innerHTML
} else {
if (window.Quarto?.typesetMath) {
window.Quarto.typesetMath(note);
}
return note.innerHTML;
}
} else {
// Remove any anchor links if they are present
const anchorLink = note.querySelector('a.anchorjs-link');
if (anchorLink) {
anchorLink.remove();
}
if (window.Quarto?.typesetMath) {
window.Quarto.typesetMath(note);
}
// TODO in 1.5, we should make sure this works without a callout special case
if (note.classList.contains("callout")) {
return note.outerHTML;
} else {
return note.innerHTML;
}
}
}
for (var i=0; i<xrefs.length; i++) {
const xref = xrefs[i];
tippyHover(xref, undefined, function(instance) {
instance.disable();
let url = xref.getAttribute('href');
let hash = undefined;
if (url.startsWith('#')) {
hash = url;
} else {
try { hash = new URL(url).hash; } catch {}
}
if (hash) {
const id = hash.replace(/^#\/?/, "");
const note = window.document.getElementById(id);
if (note !== null) {
try {
const html = processXRef(id, note.cloneNode(true));
instance.setContent(html);
} finally {
instance.enable();
instance.show();
}
} else {
// See if we can fetch this
fetch(url.split('#')[0])
.then(res => res.text())
.then(html => {
const parser = new DOMParser();
const htmlDoc = parser.parseFromString(html, "text/html");
const note = htmlDoc.getElementById(id);
if (note !== null) {
const html = processXRef(id, note);
instance.setContent(html);
}
}).finally(() => {
instance.enable();
instance.show();
});
}
} else {
// See if we can fetch a full url (with no hash to target)
// This is a special case and we should probably do some content thinning / targeting
fetch(url)
.then(res => res.text())
.then(html => {
const parser = new DOMParser();
const htmlDoc = parser.parseFromString(html, "text/html");
const note = htmlDoc.querySelector('main.content');
if (note !== null) {
// This should only happen for chapter cross references
// (since there is no id in the URL)
// remove the first header
if (note.children.length > 0 && note.children[0].tagName === "HEADER") {
note.children[0].remove();
}
const html = processXRef(null, note);
instance.setContent(html);
}
}).finally(() => {
instance.enable();
instance.show();
});
}
}, function(instance) {
});
}
let selectedAnnoteEl;
const selectorForAnnotation = ( cell, annotation) => {
let cellAttr = 'data-code-cell="' + cell + '"';
let lineAttr = 'data-code-annotation="' + annotation + '"';
const selector = 'span[' + cellAttr + '][' + lineAttr + ']';
return selector;
}
const selectCodeLines = (annoteEl) => {
const doc = window.document;
const targetCell = annoteEl.getAttribute("data-target-cell");
const targetAnnotation = annoteEl.getAttribute("data-target-annotation");
const annoteSpan = window.document.querySelector(selectorForAnnotation(targetCell, targetAnnotation));
const lines = annoteSpan.getAttribute("data-code-lines").split(",");
const lineIds = lines.map((line) => {
return targetCell + "-" + line;
})
let top = null;
let height = null;
let parent = null;
if (lineIds.length > 0) {
//compute the position of the single el (top and bottom and make a div)
const el = window.document.getElementById(lineIds[0]);
top = el.offsetTop;
height = el.offsetHeight;
parent = el.parentElement.parentElement;
if (lineIds.length > 1) {
const lastEl = window.document.getElementById(lineIds[lineIds.length - 1]);
const bottom = lastEl.offsetTop + lastEl.offsetHeight;
height = bottom - top;